Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help: Normalized Velocity Stops RigidBody's Gravity

Discussion in 'Scripting' started by Mr. Hobo, Jun 27, 2014.

  1. Mr. Hobo

    Mr. Hobo

    Joined:
    Feb 15, 2014
    Posts:
    2
    I've been using the code below to normalize my object's magnitude. It works except for the fact that when the object is moving, the rigidbody fails to fall due to gravity.

    My question is how can I modify this code so that it doesn't affect the y-component of the object's velocity?

    function Update ()
    {
    if (rigidbody.velocity.magnitude > speed)
    {
    rigidbody.velocity = rigidbody.velocity.normalized * speed;
    }
    }
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    You should treat the gravity component separately. If it's just the Y component then that's pretty easy - save the Y component before you clamp the velocity and put it back afterwards.
     
    Mr. Hobo likes this.
  3. Mr. Hobo

    Mr. Hobo

    Joined:
    Feb 15, 2014
    Posts:
    2
    Wow that was a remarkably easy fix. Thanks!
     
    angrypenguin likes this.