#Gravity problem

1 messages · Page 1 of 1 (latest)

midnight sigil
#

When I let the object freefall without any obstructions it falls at a normal pace. However, when I drive off of a platform, it suddenly falls way faster. Why is this happening and how do I fix this?


public class ShipPhysics : MonoBehaviour
{
    public float MaxSpeed;
    public float Acceleration;
    public float Deceleration;
    public float Drag;
    public float Turning;
    public float MaxBackwardsSpeed;
    public float Speed;
    public Rigidbody rb;
    void Update()
    {
        if (Input.GetAxis("Vertical") > 0 && Speed < MaxSpeed)
        {
            Speed += Acceleration * Time.deltaTime;
        }
        if (Speed > MaxSpeed)
            Speed = MaxSpeed;
        if (Input.GetAxis("Vertical") < 0 && Speed > MaxBackwardsSpeed && Speed > 0)
        {
            Speed -= Deceleration * Time.deltaTime;
        }
        if (Input.GetAxis("Vertical") < 0 && Speed > MaxBackwardsSpeed && Speed < 0)
        {
            Speed -= Acceleration * Time.deltaTime;
        }
        if (Speed < MaxBackwardsSpeed)
            Speed = MaxBackwardsSpeed;
        if (Input.GetAxis("Vertical") == 0 && Speed > 0)
        {
            Speed -= Drag * Time.deltaTime;
        }
        rb.velocity = transform.forward * Speed * -1 + new Vector3(0, rb.velocity.y, 0);
        rb.angularVelocity = Vector3.zero;
        transform.Rotate(0, Turning * Input.GetAxis("Horizontal") * Time.deltaTime, 0);
        rb.angularVelocity = Vector3.zero;
    }
}```
<@&823670198958948373>
verbal mantle
#

Are you using transform.translate to move stuff?

midnight sigil
#

No. The ship is the only thing that's moving.

#

The problem goes away if I constrain the X and Z rotations, but I don't want to do that.

midnight sigil
#

If the ship soes on an upward slant, the ship flies upwards when moving forward.

void flint
#

First off you shouldnt ever use transform.Translate or transform.Rotate with physics, you'll completely destroy your physics simulation, secondly you should do movement related code in Fixed Update
You shouldn't make those values public make them[SerializeField] private
Then check in your inspector with the object selected, see what values seem to go rogue when you drive off the edge, I have a feeling downwards velocity is being constantly added and never removed but not sure, haven't looked too deep into the code

midnight sigil
#

angularVelocity behaves weirdly though. Also I need to make all the values public so that the stats screen displays the values of the ships.

void flint
#

Then make a wrapper class/struct and supply that to some initialization method, making fields public is a terrible idea and against C# conventions
angularVelocity probably behaves exactly how it should, you're likely just not using it correctly

midnight sigil
#

Also the only value that goes rogue is the Y position.

void flint
#

Check the properties on your rigidbody in edit mode in the inspector

midnight sigil
#

The only thing weird is that the Y velocity massively increases when I fall off of a block

#

Nvm, angular velocity behaves how it should. Just didn't realize it measured in radians rather than degrees.

#

I think it's got to do with the + new Vector3(0, rb.velocity.y, 0) my way of adding gravity back in. The problem only appears when that's there.

void flint
#

Yeah, as I said check your rigidbodies properties while the inspector is in edit mode

#

Just look at its y velocity

midnight sigil
#

It goes wild when I drive up or down.

midnight sigil
#

This is an updated script, how do I solve this problem now?

public class ShipPhysics : MonoBehaviour
{
    public float MaxSpeed;
    public float Acceleration;
    [SerializeField] float Deceleration;
    [SerializeField] float Drag;
    public float Turning;
    [SerializeField] float MaxBackwardsSpeed;
    public float Speed;
    [SerializeField] Rigidbody rb;
    void Update()
    {
        if (Input.GetAxis("Vertical") > 0 && Speed < MaxSpeed)
        {
            Speed += Acceleration * Time.deltaTime;
        }
        if (Speed > MaxSpeed)
            Speed = MaxSpeed;
        if (Input.GetAxis("Vertical") < 0 && Speed > MaxBackwardsSpeed && Speed > 0)
        {
            Speed -= Deceleration * Time.deltaTime;
        }
        if (Input.GetAxis("Vertical") < 0 && Speed > MaxBackwardsSpeed && Speed < 0)
        {
            Speed -= Acceleration * Time.deltaTime;
        }
        if (Speed < MaxBackwardsSpeed)
            Speed = MaxBackwardsSpeed;
        if (Input.GetAxis("Vertical") == 0 && Speed > 0)
        {
            Speed -= Drag * Time.deltaTime;
        }
    }
    private void FixedUpdate()
    {
        rb.velocity = transform.forward * Speed * -1 + new Vector3(0, rb.velocity.y);
        rb.angularVelocity = new Vector3(0, Turning * Input.GetAxis("Horizontal") * Mathf.Deg2Rad, 0);
    }
}```
<@&823670198958948373>
sinful widget
#

have you ever heard of waiting a second

#

it's in your name

#

you don't have to ping as soon as you ask a question

#

now let's see

#

what's the problem now?

#

same?

midnight sigil
#

Yes.

sinful widget
#

you forgot a 0 in your Vector3 initializer

primal kernel
sinful widget
#

when adding y back in, you just do Vector3(0, velocity.y)

primal kernel
#

That is valid

sinful widget
#

nvm then

#

I have no ideas

midnight sigil
sinful widget
#

yeah I was wrong

lost trellis
#

to me it seems like your script is building up y velocity as the player is on the platform, since when i let the ship sit there and fall off on it's own it fell faster than if i drive the ship off the platform myself

#

my first guess would be to move new Vector3(0, rb.velocity.y) into it's own if statement that only adds it to rb.velocity.y if the player is grounded

midnight sigil
#

Nope, behaves even weirder

midnight sigil
#

<@&823670198958948373>

marsh ginkgo
#
using UnityEngine;

public class ShipPhysics : MonoBehaviour
{
    public float MaxSpeed;
    public float Acceleration;
    public float Deceleration;
    public float Drag;
    public float Turning;
    public float MaxBackwardsSpeed;
    public Rigidbody rb;

    void Update()
    {
        // Get input for acceleration and turning
        float verticalInput = Input.GetAxis("Vertical");
        float horizontalInput = Input.GetAxis("Horizontal");

        // Apply forward and backward forces based on the input
        if (verticalInput > 0 && rb.velocity.magnitude < MaxSpeed)
        {
            rb.AddForce(transform.forward * Acceleration * Time.deltaTime, ForceMode.Acceleration);
        }
        else if (verticalInput < 0 && rb.velocity.magnitude > MaxBackwardsSpeed)
        {
            rb.AddForce(transform.forward * -Deceleration * Time.deltaTime, ForceMode.Acceleration);
        }

        // Apply drag to slow down the object when not accelerating
        if (Mathf.Approximately(verticalInput, 0) && rb.velocity.magnitude > 0)
        {
            rb.AddForce(-rb.velocity.normalized * Drag * Time.deltaTime, ForceMode.VelocityChange);
        }

        // Limit the object's speed to the maximum values
        rb.velocity = Vector3.ClampMagnitude(rb.velocity, Mathf.Max(MaxSpeed, MaxBackwardsSpeed));

        // Apply rotation based on the horizontal input
        transform.Rotate(0, Turning * horizontalInput * Time.deltaTime, 0);
    }
}
#

@midnight sigil

#

The reason the object falls faster when you drive off the platform is likely due to the way the physics and movement are handled in your script. In the provided script, the object's speed is directly controlled by the input and acceleration values, without taking gravity into account. When you drive off the platform, the gravity starts acting on the object, leading to a sudden increase in the downward acceleration, resulting in faster falling.

To fix this issue, you need to incorporate the physics engine's gravity and let it handle the object's fall naturally. Instead of directly controlling the speed, you can use the Rigidbody component to apply forces to the object.

midnight sigil
#

Behaves even weirder with this script

midnight sigil
#

<@&823670198958948373>

verbal mantle
#

I'm here

#

Umm lemme read

acoustic fjord
verbal mantle
#

.addforce*

midnight sigil
#

This script seems to work fine, thanks for giving me advice!


public class ShipPhysics : MonoBehaviour
{
    public float MaxSpeed;
    public float Acceleration;
    public float Deceleration;
    public float Drag;
    public float Turning;
    public float MaxBackwardsSpeed;
    public float Speed;
    public Rigidbody rb;
    public bool IsGrounded;
    public float CheckDistance;
    void Update()
    {
        rb.angularVelocity = new Vector3(0, Turning * Input.GetAxis("Horizontal") * Time.deltaTime * Mathf.Deg2Rad, 0);
        IsGrounded = Physics.Raycast(transform.position, -transform.up, CheckDistance, LayerMask.GetMask("Ground"));
        if (Input.GetAxis("Vertical") > 0 && Speed < MaxSpeed)
        {
            Speed += Acceleration * Time.deltaTime;
        }
        else if (Input.GetAxis("Vertical") < 0 && Speed > MaxBackwardsSpeed)
        {
            Speed -= Deceleration * Time.deltaTime;
        }
        else if (Speed > 0)
        {
            Speed -= Drag * Time.deltaTime;
        }
        else if (Speed < 0)
            Speed += Drag * Time.deltaTime;
        rb.AddRelativeForce(0, 0, -Speed * Time.deltaTime, ForceMode.VelocityChange);
    }
}```
acoustic fjord
sinful widget
#

wait there's a gravity problem?