【问题标题】:Player rotation by using touch joystick not working使用触摸操纵杆的玩家旋转不起作用
【发布时间】: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.VerticalJoystick2.Horizontal。如果用户离开操纵杆回到它的初始位置,我想这些值会变成 0,因为函数在Update(),这意味着它会立即更新为 0 的旋转。
  • 您能提出解决方案吗?

标签: unity3d game-development


【解决方案1】:

当您停止触摸时,旋转恢复到默认值是有道理的,因为它是操纵杆位置和更新中调用的函数。从您的代码:

float horizontal = Joystick2.Vertical;
float vertical = Joystick2.Horizontal;

frameMovement = new Vector3(-horizontal, 0f, vertical);

我会尝试用touchPhase 处理LookAround(); 调用,这样当您调用LookAround() 时只在触摸阶段而不是所有时间。

【讨论】:

  • 但这会解决旋转重置问题吗?
  • 这是我的建议。这就是我会尝试的。如果您处理LookAround() 仅在触摸操纵杆时运行,您可以检查相机是否保留最后一次查看。如果它不起作用,您可以处理将相机的旋转保持在一个变量中并在触摸阶段结束时重新设置它
  • 非常感谢这个工作! public void Update() { if (Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(0).position.x > Screen.width / 2) { LookAround(); } }
  • 很高兴有帮助 ;)
猜你喜欢
  • 2021-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多