问题描述
我正在为 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()
。