I. Jones
I. Jones

Reputation: 151

Why is the bounce of the player inconsistent?

I am pretty new to Unity so how I did it may not be the best way, but when the character jumps it is inconsistent.

Some bounces are a lot further than others and I am not sure why this is happening.

Also if I press the up arrow really quickly the cube jumps really far but if I wait a couple seconds it bounce like normal.

Here is my code:

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;
    }
}

Upvotes: 0

Views: 126

Answers (1)

Yannik H
Yannik H

Reputation: 46

Instead of translate the object, make use of the rigidbodys ability to add a force to it. I also replaced the collision events with a simpler way. It checks the distance between the objects center and the ground. (You can still use your way obviously). The FixedUpdate insure that the code will be called regularly instead of depending on the frame speed. (More on that topic Here)

//PUBLIC
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));
        }
    }
}

public float distance

Is the min distance to the ground until you can jump again


public float fspeed

Is the forward or side speed (jump distance)


public float uspeed

Is the upwards speed (jump height)

Upvotes: 1

Related Questions