![](/img/trans.png)
[英]How can I get my camera to stop following my player when I jump on the Y axis in Unity?
[英]How can I get my player to jump?
我一直试图让我的玩家向前跳一会儿,但我被卡住了。 我查看了其他答案并进行了尝试,但后来无法按我希望的方式工作。 我尝试了几种不同的方法,但是没有一种可以按照预期的方式工作。 大多数情况下,弹跳的距离不一样,有时当玩家降落在地面上时,玩家会向前滑动,这不是我想要的。 我想让我的角色像在Crossy路上一样跳跃。 这是我尝试过的一件事:
using UnityEngine;
using System.Collections;
public class MovePlayer : MonoBehaviour
{
Vector3 endPos;
int numBackwards = 0;
bool jumping = false;
public Rigidbody rigidBody;
//public Collider theCollider;
void Start()
{
rigidBody = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update()
{
rigidBody.freezeRotation = true;
endPos = gameObject.transform.position;
if (!jumping)
{
if (Input.GetButtonDown("up") && gameObject.transform.position == endPos) {
if (numBackwards < 0)
{
numBackwards++;
}
else
{
UpdateScore.score++;
}
transform.Translate(Vector3.up * 50 * Time.deltaTime, Space.World);
transform.Translate(Vector3.forward * 110 * Time.deltaTime, Space.World);
}
else if (Input.GetButtonDown("down") && gameObject.transform.position == endPos)
{
transform.Translate(Vector3.up * 50 * Time.deltaTime, Space.World);
transform.Translate(-Vector3.forward * 110 * Time.deltaTime, Space.World);
numBackwards--;
}
else if (Input.GetButtonDown("left") && gameObject.transform.position == endPos)
{
transform.Translate(Vector3.up * 50 * Time.deltaTime, Space.World);
transform.Translate(Vector3.left * 110 * Time.deltaTime, Space.World);
}
else if (Input.GetButtonDown("right") && gameObject.transform.position == endPos)
{
transform.Translate(Vector3.up * 50 * Time.deltaTime, Space.World);
transform.Translate(Vector3.right * 110 * Time.deltaTime, Space.World);
}
}
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Ground"))
jumping = false;
}
void OnCollisionExit(Collision other)
{
if (other.gameObject.CompareTag("Ground"))
jumping = true;
}
}
这行不通,因为几乎所有的跳跃都是不同的大小,我需要它们保持一致。 我尝试过的另一件事是:
public float distance;
public float fspeed;
public float uspeed;
//PRIVATE
private Rigidbody rigidBody;
void Awake()
{
rigidBody = GetComponent<Rigidbody>();
rigidBody.freezeRotation = true;
}
void FixedUpdate()
{
if (Physics.Raycast(transform.position, -Vector3.up, distance + 0.1F))
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
rigidBody.AddForce(new Vector3(0, uspeed, fspeed));
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
rigidBody.AddForce(new Vector3(0, uspeed, -fspeed));
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
rigidBody.AddForce(new Vector3(fspeed, uspeed, 0));
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
rigidBody.AddForce(new Vector3(-fspeed, uspeed, 0));
}
}
}
这也不起作用,当玩家着陆时它将向前滑动。 我尝试的最后一件事是使用具有跳转和空闲状态的动画,我无法弄清为什么它不起作用,但是那也不起作用。 我想知道的是什么是我不能让我的播放器每次都跳相同距离而不滑动的最佳方法,或者如何固定以前的方法使它们按我的意愿工作。
使用ForceMode
。 Impulse
非常适合跳跃角色,使用质量看起来很逼真。 而且它不是连续的。 还需要较低的值才能发挥更大的作用。
if (Input.GetKey(KeyCode.UpArrow))
{
rigidBody.AddForce(new Vector3(0, uspeed, fspeed), ForceMode.Impulse);
}
离开警队模式参数空使Force
成为默认为ForceMode
这是连续的。 这比惯性造成的滑动更多。
为了忽略惯性滑动,在接触地面时取消速度。
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Ground")
{
jumping = false;
rigidBody.velocity = Vector3.zero;
}
}
在输入之前检查!jumping
可以避免空中跳跃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.