Why does my player fall very slowly because of script?

65 Views Asked by At

I have a problem with when my player needs to fall. The player is falling very slowly. I got that problem is in my script because when I turn off the script, the player is falling in normal acceleration.

Can you help me? I tried to figure out the problem in my code and tried to use ChatGPT for help, but I don't understand. I am new at Unity. Here is my code:

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class Player : MonoBehaviour
{
    Rigidbody rb;
    public GameObject trigger;
    public GameObject camera;
    public float speed;
    public float rotationSpeed;
    public Animator anim;
    public float jump;
    public bool isGrounded = true;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

---
    // Update is called once per frame
    void FixedUpdate()
    {

        


        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        // Calculate the movement direction based on input
        Vector3 cameraForward = Vector3.ProjectOnPlane(camera.transform.forward, Vector3.up).normalized;
        Vector3 cameraRight = Vector3.ProjectOnPlane(camera.transform.right, Vector3.up).normalized;
        Vector3 moveDirection = cameraForward * verticalInput + cameraRight * horizontalInput;
        moveDirection.Normalize();

        if (moveDirection != Vector3.zero)
        {
            // Calculate the target rotation based on the movement direction
            Quaternion targetRotation = Quaternion.LookRotation(moveDirection, Vector3.up);

            // Smoothly rotate the character towards the target rotation
            transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
        }

        // Move the character
        rb.velocity = moveDirection * speed;


        if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
        {
            anim.SetInteger("swr", 1);
        }
        else
        {
            anim.SetInteger("swr", 0);
        }

        
    }

    //private void OnCollisionEnter(Collision collision)
    //{
      //  if (trigger.CompareTag("Cloud"))
      //  {
             //   isGrounded = true;
     //   }
     //   else
     ///   {
      //      isGrounded = false;
      //  }
    //}

}
2

There are 2 best solutions below

0
derHugo On BEST ANSWER

You do

 rb.velocity = moveDirection * speed;

which completely ignores gravity. You copletely overrule the velocity with the new one which is only based on input

You should probably maintain the Y axis component and do e.g.

var velocity = moveDirection * speed;
velocity.y = rb.velocity.y;
rb.velocity = velocity;

Additionally you also shouldn't use

transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
  

but rather also for that one go through

rb.MoveRotation(Quaternion.RotateTowards(rb.rotation, targetRotation, rotationSpeed * Time.deltaTime));
0
Zimano On

Do not set the (vertical) velocity of the RigidBody if you want to use the physics engine to make things fall.

You probably want to use RigidBody.AddForce instead of setting the velocity to move the character. You can read more about the difference here.

The relevant explanation there is given by user Meka_Games:

Both rigidbody.velocity and rigidbody.AddForce() can be used for moving a Rigidbody in Unity, but they have different use cases.

rigidbody.velocity sets the velocity of the Rigidbody directly, meaning it will move at a constant speed in the given direction until acted upon by another force. This is useful for simple movements like sliding or bouncing, where the object will move at a fixed speed until it hits something.

rigidbody.AddForce() applies a force to the Rigidbody, causing it to accelerate in the given direction. This allows for more complex movements and interactions with other objects, as the Rigidbody will respond to other forces in the environment. You can also apply a force over a period of time using rigidbody.AddForce() and change the direction and magnitude of the force during the movement.