![](/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.