0
\$\begingroup\$

I have a character that moves with a rigidbody.

When I jump, it looks fine, but sometimes, my character boosts up while in the air and shoots out of the map very quickly.

I checked, and when it happens, it says the velocity is at about 210.

Why is this happening and how can iIfix it?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerManager : MonoBehaviour
{
    float xMovement;
    float zMovement;
    float xPosition;
    float yPosition;
    float zPosition;
    public float runningSpeed;
    public Animator animator;
    public Camera camera;
    public float cameraHeight;
    public float cameraAngle;
    public bool isOnGround;
    public float jumpForce;
    Rigidbody playerRigidbody;
    public float gravityMultiplier;
    Vector3 vel;
    public float airSpeed;
    public float groundSpeed;
    public bool canJump = true;

    void Start()
    {
        playerRigidbody = GetComponent<Rigidbody>();
        vel = playerRigidbody.velocity;
    }

    void Update()
    {

        xPosition = transform.position.x;
        yPosition = transform.position.y;
        zPosition = transform.position.z;
        PlayerMovements();      
        CameraManager();
    }


    public void CameraManager()
    {
        camera.transform.position = new Vector3(xPosition+cameraAngle,cameraHeight,zPosition+cameraAngle);
    }


    public void PlayerMovements()
    {
        //Jump
        if(Input.GetKeyDown(KeyCode.Space) & isOnGround == true & canJump == true)
        {
            runningSpeed = airSpeed;
            canJump = false;
            playerRigidbody.velocity = new Vector3(xMovement,0,zMovement);
            playerRigidbody.angularVelocity = Vector3.zero;
            playerRigidbody.constraints &= ~RigidbodyConstraints.FreezePositionY;
            playerRigidbody.AddForce(transform.up * jumpForce);
            playerRigidbody.drag = 0;

        }


        //Move the Character
        if(isOnGround == true)
        {
            xMovement = Input.GetAxis ("Horizontal");
            zMovement = Input.GetAxis ("Vertical");
            Vector3 distanceToMove = new Vector3(xMovement,0,zMovement);
            playerRigidbody.AddForce(distanceToMove * runningSpeed);
        }


        //Make the player face the direction he's going at
        transform.LookAt(new Vector3(xPosition+xMovement,yPosition,zPosition+zMovement));



        //Trigger Idle or Running Animations
        if(xMovement != 0 | zMovement != 0)
        {
            animator.SetBool("Run", true);
            animator.SetBool("Idle", false);
        }
        if((xMovement < 0.05f & xMovement > -0.05f) & (zMovement < 0.05f & zMovement > -0.05f))
        {
            animator.SetBool("Run", false);
            animator.SetBool("Idle", true);
        }           
    }


    public void OnCollisionEnter(Collision collidedOBJ)
    {
        if(collidedOBJ.gameObject.tag=="Finish")
        {
            Debug.Log(playerRigidbody.velocity);
            Debug.Log(runningSpeed);
        }
        if(collidedOBJ.gameObject.name == "Ground")
        {
            playerRigidbody.drag = 49;
            playerRigidbody.constraints &= RigidbodyConstraints.FreezePositionY|RigidbodyConstraints.FreezeRotationX|RigidbodyConstraints.FreezeRotationY|RigidbodyConstraints.FreezeRotationZ;
            isOnGround = true;
            canJump = true;
            runningSpeed = groundSpeed;


        }
    }

    public void OnCollisionExit(Collision exitCollidedOBJ)
    {
        if(exitCollidedOBJ.gameObject.name == "Ground")
        {
            isOnGround = false;

        }
    }

}
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Interaction with physics should be done inside FixedUpdate(), not Update(). See https://stackoverflow.com/questions/34447682/what-is-the-difference-between-update-fixedupdate-in-unity

If you add force in Update(), the amount of added force will depend on the frame rate. If you have irregular frame rate, you will get unpredictable physics.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .