Home > Blockchain >  Unity3D - Gravity Not Applying When Activating my Movement Script
Unity3D - Gravity Not Applying When Activating my Movement Script

Time:09-23

Hi ! :) Sooo I kinda have a problem in Unity3d :

I use the following script to make my character move, my character only has a RigidBody, Capsule Collider and this Script (CharacterControllerCode.cs) attached. When i leave the script unchecked, the gravity applies as it should to the character but when I activate the script, the grvity does not apply anymore and the Character stays at the Y position assigned in the editor (Transform Component).

Does anyone have the smallest clue why this is happening ?

(i'm very new to c# and unity so i checked out this video to make the code https://www.youtube.com/watch?v=WIl6ysorTE0&list=PLYQCqA5CcgADEN4gGw7qdsqcN9ERUrHbM∈dex=19)

I'm using the New Input System Package btw

Script below ↓

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

public class PlayerControllerCode : MonoBehaviour
{
   private PlayerControlInputs playerControlsAsset;
   private InputAction moveAction;

   private Rigidbody rigBod;
   
   [SerializeField]
   private float movementForce = 1f;

   
   [SerializeField]
   private float maxSpeed = 5f;

   [SerializeField]
   private float jumpForce = 5f;

   [SerializeField]
   private float dashForce = 5f;

   private Vector3 forceDirection = Vector3.zero;

   [SerializeField]
   private Camera playerCamera;
private void Awake() 
{
    rigBod = this.GetComponent<Rigidbody>();
    playerControlsAsset = new PlayerControlInputs();
}

private void OnEnable() 
{
    playerControlsAsset.PlayerActions.Jump.started  = DoJump;
    playerControlsAsset.PlayerActions.Dash.started  = DoDash;
    moveAction = playerControlsAsset.PlayerActions.Move;
    playerControlsAsset.PlayerActions.Enable(); 
}
   
private void OnDisable()
{
    playerControlsAsset.PlayerActions.Jump.started -= DoJump;
    playerControlsAsset.PlayerActions.Dash.started -= DoDash;
    playerControlsAsset.PlayerActions.Disable();  
}

    
    private void FixedUpdate() {

    LookAt();
    IsGrounded();
    forceDirection  = moveAction.ReadValue<Vector2>().x * GetCameraRight(playerCamera) * movementForce;
    forceDirection  = moveAction.ReadValue<Vector2>().y * GetCameraForward(playerCamera) * movementForce;

    rigBod.AddForce(forceDirection, ForceMode.Impulse);
    forceDirection = Vector3.zero;

    Vector3 horizontalVelocity = rigBod.velocity;
    horizontalVelocity.y = 0;

    if(rigBod.velocity.y < 0f)
    {
        rigBod.velocity  = Vector3.down * Physics.gravity.y * Time.fixedDeltaTime;
    }

    if(horizontalVelocity.sqrMagnitude > maxSpeed * maxSpeed)
    {
        rigBod.velocity = horizontalVelocity.normalized * maxSpeed   Vector3.up * rigBod.velocity.y;
    }

    


}

private void LookAt()
{
    Vector3 direction = rigBod.velocity;
    direction.y = 0f;

    if(moveAction.ReadValue<Vector2>().sqrMagnitude > 0.1f && direction.sqrMagnitude > 0.1f)
    {
        this.rigBod.rotation = Quaternion.LookRotation(direction, Vector3.up);

    }
    else 
    {
        rigBod.angularVelocity = Vector3.zero;

    }

}

private Vector3 GetCameraRight(Camera playerCamera)
{
    Vector3 right = playerCamera.transform.right;
        right.y = 0;
        return right.normalized;
}

private Vector3 GetCameraForward(Camera playerCamera)
{
        Vector3 forward = playerCamera.transform.forward;
        forward.y = 0;
        return forward.normalized;
}

    private void DoJump(InputAction.CallbackContext obj)
{
    if(IsGrounded())
    {
        forceDirection  = Vector3.up * jumpForce;

    }
}



private void DoDash(InputAction.CallbackContext obj)
{
        forceDirection  = moveAction.ReadValue<Vector2>().y * GetCameraForward(playerCamera) * dashForce;
}


private bool IsGrounded()
{
    Ray ray = new Ray(this.transform.position   Vector3.up * 0.05f, Vector3.down);
    if(Physics.Raycast(ray, out RaycastHit hit, 0.1f))
     {return true;}
    else
     {return false;}

}
}

My Hierarchy

My Character's Components

CodePudding user response:

So turns out i was just being blind and there was a simple sign error :

Instead of add ( =) here :

if(rigBod.velocity.y < 0f)

    {
        rigBod.velocity  = Vector3.down * Physics.gravity.y * Time.fixedDeltaTime;
    }

It should have been substract (-=)

if(rigBod.velocity.y < 0f)

    {
        rigBod.velocity -= Vector3.down * Physics.gravity.y * Time.fixedDeltaTime;
    }

CodePudding user response:

when you set the rigidbody's velocity from the script, it overrides the velocity calculated by the rigidbody.

You should either use your own physics calculation or only apply forces to unity's rigidbody, but don't do both as it creates this kind of conflict.

  • Related