繁体   English   中英

播放器对象在旋​​转时改变位置

[英]Player object changes position while rotating

首先:对不起,我的英语不好,我是荷兰人。

我正在尝试创建一个播放器对象,该对象可以前后移动(使用向上和向下键),并使用向右和向左键旋转播放器。 但是,当我按向左或向右键时,播放器的位置会发生变化,看起来它围绕某个点旋转。 但是它应该旋转并保持在同一位置,就像我转身时一样。

我还有其他一些小程序,具有相同的“移动脚本”和相同的inputmanager设置。 在那里工作正常,所以我不知道为什么这次不起作用。

这是我的移动脚本,但是此脚本可与其他程序配合使用:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveScript : MonoBehaviour
{

    public float speed = 3f;
    public float rotate = 50f;
    private Rigidbody rb;
    // Update is called once per frame

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
    void Update()
    {
        if (Input.GetButton("Up"))
            transform.Translate(Vector3.forward * speed * Time.deltaTime);
        if (Input.GetButton("Down"))
            transform.Translate(-Vector3.forward * speed * Time.deltaTime);
        if (Input.GetButton("Right"))
            transform.Rotate(Vector3.up, rotate * Time.deltaTime);
        if (Input.GetButton("Left"))
            transform.Rotate(-Vector3.up, rotate * Time.deltaTime);
        if (gameObject.GetComponent<Rigidbody>().velocity.y < 0.01)
        {
            if (Input.GetButtonDown("Jump"))
                rb.AddForce(0, 225, 0);
        }

    }

}

InputManager设置(也可以与其他运行正常的程序相同):

在此处输入图片说明

在此处输入图片说明

如果有人想要我程序的其他内容的屏幕截图,您当然可以随时提出要求。 我不知道问题出在什么地方,如果它不在脚本或输入管理器中。

根据OP中的注释,解决方案应使用:

if (Input.GetButton("Right"))
    transform.localEulerAngles = transform.localEulerAngles + new Vector3(xRotation, 0, 0) * Time.deltaTime;
if (Input.GetButton("Left"))
    transform.localEulerAngles = transform.localEulerAngles - new Vector3(xRotation, 0, 0) * Time.deltaTime;

其中xRotation是一个浮点数,当旋转键之一按下时,您要每秒旋转的量。

(PS:目前尚无统一性,因此+-可能会颠倒,但应类似这样。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM