【发布时间】:2021-07-06 06:53:28
【问题描述】:
我正在为 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;
}
}
【问题讨论】:
-
在每一帧,Unity 都使用
Update()方法来运行。您正在调用LookAround(),而后者又使用Joystick2.Vertical和Joystick2.Horizontal。如果用户离开操纵杆回到它的初始位置,我想这些值会变成 0,因为函数在Update(),这意味着它会立即更新为 0 的旋转。 -
您能提出解决方案吗?