我正在创建一个简单的第二场游戏来学习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)
);
}
}
答案 0 :(得分:0)
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;
//}
}