我是Unity和Stack Overflow的新手,一直在寻找一个可以让对象(比如玩家)移动的脚本。我已经找到了一个可以工作的脚本,嗯,某种工作或者说是按计划进行的。当我测试脚本时,当我按下箭头键时,它就会开始跳跃,而不是前进。如果我按下向下箭头键,魔方(或玩家)将试图把自己推到地下,然后永远掉下去,但左右箭头键工作得很好。请注意,这个脚本现在只用于移动播放器,没有其他东西,只是以防你认为它应该是其他或不同的东西。代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
public float moveSpeed;
// Use this for initialization
void Start()
{
moveSpeed = 5f;
}
// Update is called once per frame
void Update()
{
transform.Translate(moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, moveSpeed * Input.GetAxis("Vertical") * Time.deltaTime, 0f);
}
}
我希望你能找到一个解决方案或找到一个解释。感谢您的回复。致敬,用户:9104031
发布于 2018-01-04 03:09:51
Unity使用一个坐标系,其中Y向上,Z向前。
在你的代码中,你基于Y轴上的“垂直”轴输入来移动你的玩家,这当然会将你的向上/向下方向键映射到错误的方向。
你所要做的就是改变
transform.Translate(
moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime
, moveSpeed * Input.GetAxis("Vertical") * Time.deltaTime
, 0f);
至
transform.Translate(
moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime
, 0f
, moveSpeed * Input.GetAxis("Vertical") * Time.deltaTime);
发布于 2018-01-04 03:07:05
如果我没理解错的话,我相信这就是你在更新部分想要的:
transform.Translate(moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, moveSpeed * Input.GetAxis("Vertical") * Time.deltaTime);
我调换了你的Y轴和Z轴。希望这就是你想要的!
https://stackoverflow.com/questions/48087882
复制相似问题