我正在嘗試通過執行以下操作來移動物件:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed;
// Update is called once per frame
void Update()
{
float horznotalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector3 movment = new Vector3(horznotalInput,0,verticalInput);
transform.Translate(movment * Time.deltaTime *speed);
}
}
但是物體的運動發生了一些奇怪的事情。
當按下“a”鍵時,物體會上升
“d”下降。
"w" 向右
“s”向左走。
當我將其更改為以下內容時:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed;
// Update is called once per frame
void Update()
{
float horznotalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
float newHorizontalPosition = transform.position.x horznotalInput * Time.deltaTime;
float newVerticalPosition = transform.position.z verticalInput * Time.deltaTime;
transform.position = new Vector3(newHorizontalPosition,transform.position.y,newVerticalPosition);
}
}
它按預期作業。
現在,我正在學習一個教程,并且我復制了與他相同的內容。但這對我不起作用。
請問有什么幫助嗎?
謝謝!
uj5u.com熱心網友回復:
Translate默認使用本地空間!
聽起來您的物件(或層次結構中的某個父物件)旋轉了 90°。
如果你想要全域軸,你應該使用
transform.Translate(movment * Time.deltaTime * speed, Space.World);
或者為了簡化你的第二個片段,你也可以這樣做
transform.position = movment * Time.deltaTime * speed;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/409446.html
標籤:
