[英]Player object changes position while rotating
First of all: Sorry for my bad English, I'm Dutch. 首先:对不起,我的英语不好,我是荷兰人。
I'm trying to create a player object that can walk forward and backwards (with the up and down keys), and rotate the player with the right and left keys. 我正在尝试创建一个播放器对象,该对象可以前后移动(使用向上和向下键),并使用向右和向左键旋转播放器。 But when I press the left or right key, the position of the player changes, it looks like it rotates around a certain point.
但是,当我按向左或向右键时,播放器的位置会发生变化,看起来它围绕某个点旋转。 But it should rotate and stay on the same place, like I do when I turn around.
但是它应该旋转并保持在同一位置,就像我转身时一样。
I have some other small programs, with the same 'move script' and the same inputmanager settings. 我还有其他一些小程序,具有相同的“移动脚本”和相同的inputmanager设置。 There it works fine, so I have no idea why it doesn't work this time.
在那里工作正常,所以我不知道为什么这次不起作用。
This is my move script, but this script works fine with other programs: 这是我的移动脚本,但是此脚本可与其他程序配合使用:
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);
}
}
}
The InputManager settings (also the same as other programs where it works fine): InputManager设置(也可以与其他运行正常的程序相同):
If someone wants a screenshot of something else of my program, you can always ask it of course. 如果有人想要我程序的其他内容的屏幕截图,您当然可以随时提出要求。 I have no idea what the problem could be, if it isn't in the script or the inputmanager.
我不知道问题出在什么地方,如果它不在脚本或输入管理器中。
Following the comments in the OP, the solution should be to use: 根据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;
Where xRotation
is a float with the amount you want to rotate per second when and while one of the rotation keys is down. 其中
xRotation
是一个浮点数,当旋转键之一按下时,您要每秒旋转的量。
(PS: Don't have unity open right now, so +
and -
might be inverted, but should be something like that.) (PS:目前尚无统一性,因此
+
和-
可能会颠倒,但应类似这样。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.