Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question how to add speed to velocity.magnitude

Discussion in 'Getting Started' started by jermainetai0307, Mar 2, 2023.

  1. jermainetai0307

    jermainetai0307

    Joined:
    Nov 10, 2022
    Posts:
    27
    i have a ball that gets reflected the other way everytime it hits a wall.
    this is done with this.
    Code (CSharp):
    1.     void OnCollisionEnter(Collision collision)
    2.     {
    3.         bounce++;
    4.         // Magnitude of the velocity vector is speed of the object (we will use it for constant speed so object never stop)
    5.         float speed = velocity.magnitude;
    6.  
    7.         // Reflect params must be normalized so we get new direction
    8.         Vector3 direction = Vector3.Reflect(velocity.normalized, collision.contacts[0].normal);
    9.  
    10.         // Like earlier wrote: velocity vector is magnitude (speed) and direction (a new one)
    11.         rb.velocity = direction * speed;
    12.     }
    unfortunately this is almost entirely taken from somewhere else and while I do somewhat understand how it works, I don't really know how to work with it. I'm trying to use the variable "bounce" to keep track of how many times the ball has touched the wall. and I want to make it so that everytime it hits the wall, the speed of it increases. it initially starts moving with a simple rb.addforce with a velocitychange. and after that all of the movement is done with these code. i am assuming that to achieve the result i want i have to change something related to the velocity.magnitude. its just that i don't really know how to. help is appreciated :D
     
  2. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Vector.magnitude is the length of the vector.
    Unity - Scripting API: Vector3.magnitude (unity3d.com)

    Thus, in the case where the vector is your velocity (including direction, in a space), its length is the speed.

    Vector.normalized returns a vector with length 1
    Unity - Scripting API: Vector3.normalized (unity3d.com)

    Thus, normalizing the velocity will give you its direction (with a length of 1)

    Code (CSharp):
    1. // direction * length = length AND direction
    2. velocity.normalized * velocity.magnitude  == velocity;
    If you want to change the speed, you'll want to adjust the magnitude:

    velocity = (velocity.magnitude * 1.1f) * velocity.normalized; // Increase speed by 10%

    or
    velocity = (velocity.magnitude + 0.1f) * velocity.normalized; // Increase speed by 0.1 Units


    of course you can also adjust it directly, if you're only interested in changing the magnitude:

    velocity = velocity * 1.1f; // Increase speed by 10%

    or
    velocity *= 1.1f; // Increase speed by 10%
     
  3. jermainetai0307

    jermainetai0307

    Joined:
    Nov 10, 2022
    Posts:
    27
    so...while i do understand what ur saying. i do not know how...but the whole ball is broken. i logged off yesterday and I'm pretty sure it was fine and today its jus...not working... the ball is bouncing but it doesn't even bounce back at the same speed at all. it slows down tremendously, maybe I was fiddling with the code yesterday and didn't change it back or something but I was hoping u can help me take a look because it all looks fine to me.
    Code (CSharp):
    1. public class ballmove : MonoBehaviour
    2.     {
    3.     // Drag and drop Rigidbody in Inspector
    4.     public Rigidbody rb;
    5.     public Vector3 velocity;
    6.     public float bounce;
    7.  
    8.     void Start()
    9.     {
    10.         rb = GetComponent<Rigidbody>();
    11.         // Add force once at start
    12.         rb.AddForce((Vector3.forward + Vector3.left) * 10.0f, ForceMode.VelocityChange);
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         // Track velocity, it holds magnitude and direction (for collision math)
    18.         Vector3 velocity = rb.velocity;
    19.         Debug.Log("velocity:" + rb.velocity);
    20.     }
    21.  
    22.     private void FixedUpdate()
    23.     {
    24.     }
    25.  
    26.     void OnCollisionEnter(Collision collision)
    27.     {
    28.         bounce++;
    29.         // Magnitude of the velocity vector is speed of the object (we will use it for constant speed so object never stop)
    30.         float speed = velocity.magnitude;
    31.  
    32.         // Reflect params must be normalized so we get new direction
    33.         Vector3 direction = Vector3.Reflect(velocity.normalized, collision.contacts[0].normal);
    34.  
    35.         // Like earlier wrote: velocity vector is magnitude (speed) and direction (a new one)
    36.         rb.velocity = direction * speed;
    37.     }
    38. }
     
  4. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    A few things here:

    rb.AddForce((Vector3.forward + Vector3.left) * 10.0f, ForceMode.VelocityChange);

    Vector3.forward + Vector3.left has a length of Sqrt(2); (Vector3(-1, 0, 1))
    You're thus setting a starting-velocity of Sqrt(2) * 10f (+/- 14.14f)

    Vector3.Reflect(velocity.normalized, collision.contacts[0].normal);


    You're normalizing the Velocity, but not the result of Reflect. If your speed is changing, then I'm guessing the magnitude of Direction is NOT 1. You can log this to check.

    Code (CSharp):
    1. // Like earlier wrote: velocity vector is magnitude (speed) and direction (a new one)
    2. rb.velocity = direction * speed;
    If you want to ensure that your direction has no speed in it, you should normalize direction here:

    Code (CSharp):
    1. // Like earlier wrote: velocity vector is magnitude (speed) and direction (a new one)
    2. rb.velocity = direction.normalized * speed;
     
  5. jermainetai0307

    jermainetai0307

    Joined:
    Nov 10, 2022
    Posts:
    27
    I've never been good at maths....and while this might seem simple to some. I'm completed puzzled lol. so ill try and reply this that makes sense to me. if your saying that by adding Vector3.forward + Vector3.left messes up the numbers in a way because it becomes a squareroot somehow. well im assuming this won't happen if I only put Vector3.forward. but the thing is even if I only put Vector3.forward with no left, the problem still persists.

    so while I don't know how to check whether direction is 1 or not.
    i don't really know why normalizing the velocity will change the result since i got all this from a tutorial. im just assuming its how reflect works. but then when i do like you said and normalized direction, nothing really changes either.
    i also have a question regarding your earlier reply.
    is there a reason why you did it here when adjusting the velocity and not when i was defining float speed.
    like instead of what you did. could i have gone up the script a bit and instead just change the code from earlier to
    Code (CSharp):
    1. float speed = velocity.magnitude * 1.1f;
    is there a particular reason behind this, or is it all just personal preference.

    Edit: after reading your reply a little bit more. i realized how I missed out so much in your reply.
    in your reply it was
    Code (CSharp):
    1. velocity = (velocity.magnitude * 1.5f) * velocity.normalized;
    I'm assuming this was to replace my original code which was
    Code (CSharp):
    1. rb.velocity = direction * speed;
    if so then I have another question, why the velocity.normalized at the back? is that to replace the direction in some way? because wouldn't that jus make it continue in that direction with no..bounce at all? unless your code in the reply was to replace something else entirely, then I'm just dumb. sorry if I'm not getting your point here as u can tell I'm quite new;)
     
    Last edited: Mar 4, 2023