使用触摸操纵杆进行播放器旋转不起作用
Player rotation by using touch joystick not working
我正在为 android 开发这款游戏,我需要拖曳触摸操纵杆来移动和环顾四周,我已经能够使用我现在拥有的脚本来做到这一点,但是一旦我停止我就会遇到问题移动操纵杆玩家旋转恢复到其原始位置任何帮助?????
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody RB;
public FixedJoystick Joystick;
public Joystick Joystick2;
public float MoveSpeed = 300f;
public Vector3 LastPosition;
public Vector3 frameMovement;
// Start is called before the first frame update
void Start()
{
RB = GetComponent<Rigidbody>();
}
public void Update()
{
LookAround();
}
public void FixedUpdate()
{
RB.velocity = new Vector3(Joystick.Horizontal * MoveSpeed * Time.deltaTime, RB.velocity.y, Joystick.Vertical * MoveSpeed * Time.deltaTime);
}
public void LookAround()
{
float horizontal = Joystick2.Vertical;
float vertical = Joystick2.Horizontal;
frameMovement = new Vector3(-horizontal, 0f, vertical);
Quaternion rotation =
Quaternion.LookRotation(frameMovement);
RB.transform.rotation = rotation;
}
}
我觉得当您停止触摸时旋转会恢复默认值,因为它是操纵杆位置的函数,并在更新中调用。来自您的代码:
float horizontal = Joystick2.Vertical;
float vertical = Joystick2.Horizontal;
frameMovement = new Vector3(-horizontal, 0f, vertical);
我会尝试使用 touchPhase 处理 LookAround();
调用,这样当您仅在触摸阶段而不是所有时间调用 LookAround()
时。
我正在为 android 开发这款游戏,我需要拖曳触摸操纵杆来移动和环顾四周,我已经能够使用我现在拥有的脚本来做到这一点,但是一旦我停止我就会遇到问题移动操纵杆玩家旋转恢复到其原始位置任何帮助?????
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody RB;
public FixedJoystick Joystick;
public Joystick Joystick2;
public float MoveSpeed = 300f;
public Vector3 LastPosition;
public Vector3 frameMovement;
// Start is called before the first frame update
void Start()
{
RB = GetComponent<Rigidbody>();
}
public void Update()
{
LookAround();
}
public void FixedUpdate()
{
RB.velocity = new Vector3(Joystick.Horizontal * MoveSpeed * Time.deltaTime, RB.velocity.y, Joystick.Vertical * MoveSpeed * Time.deltaTime);
}
public void LookAround()
{
float horizontal = Joystick2.Vertical;
float vertical = Joystick2.Horizontal;
frameMovement = new Vector3(-horizontal, 0f, vertical);
Quaternion rotation =
Quaternion.LookRotation(frameMovement);
RB.transform.rotation = rotation;
}
}
我觉得当您停止触摸时旋转会恢复默认值,因为它是操纵杆位置的函数,并在更新中调用。来自您的代码:
float horizontal = Joystick2.Vertical;
float vertical = Joystick2.Horizontal;
frameMovement = new Vector3(-horizontal, 0f, vertical);
我会尝试使用 touchPhase 处理 LookAround();
调用,这样当您仅在触摸阶段而不是所有时间调用 LookAround()
时。