[英]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.