I have been trying to get my player to jump forward for a little while and I am stuck. I have looked at other answers and tried them but then I couldn't get it to work how I wanted it to. I have tried several different ways but none of them are working how they are supposed to. Most of the time the bounces weren't the same distance and sometimes when the player lands on the ground the player will slide forward which is not what I want. I want to have my character jump like in Crossy road. Here is one thing that I tried:
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; } } This didn't work because almost all of the jumps were different sizes and I need them to be consistent. Another thing I tried was this:
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)); } } } This didn't work either and when the player landed it would slide forward. The last thing I tried was using an animation with jump and idle which I couldn't figure out why it wasn't working but that wasn't working either. What I would like to know is what is the best way that I can't get my player to jump the same distance every time without sliding or how can I fix on of my previous ways to make them work how I want it to.