Search Unity

Custom gravity and Velocity

Discussion in 'Physics' started by slalithp, Jun 23, 2019.

  1. slalithp

    slalithp

    Joined:
    Feb 28, 2019
    Posts:
    5
    Hi, I am trying to achieve realistic movements on my object for which I have been writing a custom gravity (with gravity Switched-Off on the RigidBody Component) and velocity script, but my gravity does not act properly. Even if I Switch-On the gravity in the RigidBody Component, My gravity and jump do not work. My gravity(Scripted or from the editor) and Jump seems to work properly when I do not implement Movements. I have also tried reducing Drag below zero and yet my gravity and jump do not work. Here are the Scripts -

    Physics.cs -

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. namespace CustomPhysics
    5. {
    6.      public class PhysicsClass : MonoBehaviour
    7.      {
    8.          private static PhysicsClass _physicsInstance;
    9.          public static PhysicsClass PhysicsInstance
    10.          {
    11.              get
    12.              {
    13.                  if (_physicsInstance == null)
    14.                  {
    15.                      _physicsInstance = GameObject.FindObjectOfType<PhysicsClass>();
    16.                  }
    17.                  return _physicsInstance;
    18.              }
    19.          }
    20.          //gravity fields
    21.          private Vector3 _gravityVector;
    22.          private float _jumpAcceleration;
    23.          private float accelerationDueToGravity;
    24.          //movement fields
    25.          private float moveAcceleration;
    26.          private float friction;
    27.          private Vector3 _moveVector;
    28.          private void Awake()
    29.          {
    30.              moveAcceleration = 5;
    31.              friction = 1.5f;
    32.              accelerationDueToGravity = 9.8f;
    33.          }
    34.          public float JumpAcceleration
    35.          {
    36.              get
    37.              {
    38.                  _jumpAcceleration = 3;
    39.                  return _jumpAcceleration;
    40.              }
    41.          }
    42.          public Vector3 GravityVector
    43.          {
    44.              get
    45.              {
    46.                  _gravityVector = Vector3.down * accelerationDueToGravity;
    47.                  return _gravityVector;
    48.              }
    49.          }
    50.          public Vector3 MoveVector
    51.          {
    52.              get
    53.              {
    54.                  float x = Input.GetAxis("Horizontal");
    55.                  float y = Input.GetAxis("Vertical");
    56.                  _moveVector = (new Vector3(x, 0, y) * moveAcceleration) / friction;
    57.                  return _moveVector;
    58.              }
    59.          }
    60.      }
    61. }
    GravityAndVelocity.cs-

    Code (CSharp):
    1.  using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using static CustomPhysics.PhysicsClass;
    5. public class GravityAndVelocity: MonoBehaviour
    6. {
    7.      private Rigidbody RB;
    8.      private Transform objectTransform;
    9.      private float heightFactor;
    10.      private void Start()
    11.      {
    12.          RB = gameObject.GetComponent<Rigidbody>();
    13.          objectTransform = gameObject.GetComponent<Transform>();
    14.          heightFactor = 1.75f;
    15.      }
    16.      private void FixedUpdate()
    17.      {
    18.          //RB.drag = -100;
    19.          if (objectTransform.position.y > 0.1f)
    20.          {
    21.              gravity();
    22.              if (Input.GetKey(KeyCode.Space) && objectTransform.position.y < heightFactor)
    23.              {
    24.                  RB.velocity = Vector3.up * PhysicsInstance.JumpAcceleration; // * Time.time;
    25.              }
    26.          }
    27.          RB.velocity = (PhysicsInstance.MoveVector);
    28.      }
    29.      void gravity()
    30.      {
    31.          RB.AddForce(PhysicsInstance.GravityVector);
    32.      }
    33. }
    So, What i have done wrong? please Kindly, Explain.
     
    Last edited: Jun 23, 2019
  2. slalithp

    slalithp

    Joined:
    Feb 28, 2019
    Posts:
    5
    Answer- when space pressed - downforce += upwardforce;