用操纵杆旋转游戏对象

时间:2019-10-20 22:00:43

标签: c# android unity3d

尝试使用操纵杆旋转我的gameObject之前,我以前曾经编写过一个脚本,该脚本要求Gameobject面对mouseInput,但我希望它完全一样,但是在使用操纵杆时呢?

 public class FaceMouse : MonoBehaviour
{
void Update()
{


    if (Input.GetMouseButton(0))

    {

        Rotation();

    }

}

void Rotation()
{
    Vector3 mousePosition = Input.mousePosition;

    mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);

    Vector2 direction = new Vector2(
        mousePosition.x - transform.position.x,
        mousePosition.y - transform.position.y);

    transform.up = direction;
}
}

我正在搜索并且找到了这个脚本,但是gameobject并没有像时钟一样旋转,而是使我的gameobject在另一个轴上旋转,知道如何使我的对象像第一个脚本那样复制JOYTICK动作吗?

public class PlayerMovements : MonoBehaviour
{
public FixedJoystick joystick;
public float speed = 10f;
public float roatateSpeed = 40f;

public GameObject rb;


private void Start()
{
    rb = GetComponent<GameObject>();


}


private void Update()
{
    float horizontal = joystick.Horizontal;
    float vertical = joystick.Vertical;

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




    Quaternion rotation = Quaternion.LookRotation(frameMovement);
    transform.rotation = rotation;
}
}

1 个答案:

答案 0 :(得分:0)

尝试一下

float AxisFactor = 90;
transform.localEulerAngles += new Vector3(vertical, horizontal, 0)*AxisFactor*Time.deltaTime;