Unity中控制玩家的脚本问题

本文关键字:脚本 问题 玩家 控制 Unity | 更新日期: 2023-09-27 18:27:55

我正在创建一个简单的第二个游戏来学习Unity。我有一个在x轴上左右移动玩家的脚本。此外,我在同一脚本中添加了移动倾斜控件。然而,我有一个问题。当我玩游戏并按下D键将玩家向右移动时,它会向右移动,但我一放开,它就会向后跳1/2。我花了几个小时研究这个代码,但玩家一直跳回到x轴上的1/2方向。为什么?请帮忙,非常感谢!

using UnityEngine;
using System.Collections;
[System.Serializable]
public class BoundaryOne 
{
public float xMin, xMax, yMin, yMax;
}
public class Done_PlayerController : MonoBehaviour
{
public float speed;
public BoundaryOne boundary;

void Update ()
    {
        transform.Translate(Input.acceleration.x / 4, 0, 0);
    }
   // void Start ()
//{
    //GetComponent<Rigidbody>().velocity = transform.right * speed;
//}
void FixedUpdate ()
{
    float moveHorizontal = Input.GetAxis ("Horizontal");

    Vector3 movement = new Vector3 (moveHorizontal,0);
    GetComponent<Rigidbody>().velocity = movement * speed;
    GetComponent<Rigidbody>().position = new Vector3
    (
        Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
        Mathf.Clamp (GetComponent<Rigidbody>().position.y, boundary.yMin, boundary.yMax)
    );

}
}

Unity中控制玩家的脚本问题

using UnityEngine;
using System.Collections;
[System.Serializable]
public class BoundaryOne 
{
public float xMin, xMax, yMin, yMax;
}
public class Done_PlayerController : MonoBehaviour
{
public float speed;
//using mathf to limit bounds of movement
public BoundaryOne boundary;

void Update ()
{
    float moveHorizontal = Input.GetAxis("Horizontal");

    Vector3 movement = new Vector3(moveHorizontal, 0);
    GetComponent<Rigidbody>().velocity = movement * speed;
    GetComponent<Rigidbody>().position = new Vector3
    (
        Mathf.Clamp(GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
            Mathf.Clamp(0, 0, 0)
        );
    //my mobile tilt controls
    transform.Translate(Input.acceleration.x / 4, 0, 0);
}
// void Start ()
//{
//GetComponent<Rigidbody>().velocity = transform.right * speed;
//}
}