【问题标题】:Unity script to move a 3D object doesn't completely go to plan用于移动 3D 对象的 Unity 脚本并未完全按计划进行
【发布时间】:2018-01-04 02:31:13
【问题描述】:

我是 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

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    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);
    

    【讨论】:

      【解决方案2】:

      如果我对您的理解正确,我相信这就是您在更新部分中想要的:

      transform.Translate(moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, moveSpeed * Input.GetAxis("Vertical") * Time.deltaTime);

      我交换了你的 Y 轴和 Z 轴。希望这就是你想要的!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-14
        • 1970-01-01
        • 2019-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多