我正在为Android构建一个自上而下的游戏,游戏本身就是3D。我需要它让角色顺利地面向操纵杆的方向并向那个方向移动。我可以将操纵杆的当前方向作为矢量2.但不知道如何使用Vector 2变量旋转我的播放器。
这是我用来移动我的汽车(播放器)的代码。我使用Joystick Pack触摸操纵杆。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public FixedJoystick joystick;
public float speed = 10f;
public float rotateSpeed = 40f;
Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
float horizontal = joystick.Horizontal;
float vertical = joystick.Vertical;
Vector3 movement = transform.InverseTransformDirection(new
Vector3(horizontal, 0, vertical));
rb.velocity = (movement * speed);
}
}
先感谢您!!
frameMovement = new Vector3(horizontal, 0f,vertical);
Quaternion rotation = Quaternion.LookRotation(frameMovement);
transform.rotation = rotation;
这有助于我在操纵杆面向的地方移动和旋转。