Why dont we just slightly change direction instead of velocity?

Hi, my first post here. Loving the unity course so far.

My problem with the boring loop solution is that playing with velocity in each collision might end up too much accumulated impact on the velocity. One way is to add caps to velocity via IF statements but overall varying velocity in the ball randomly does not seem good to me. Instead can we just slightly play with the direction keeping the velocity same?

I checked unity forum and indeed it can be done but wanted to check your perspective if you intentionally did not want to do that?
here: https://forum.unity.com/threads/change-velocity-direction-and-not-magnatude.368272/

thanks!

1 Like

I would say because you want to use the physics engine for the movement to give it a more natural feel. If you just manipulate the transform it may not feel the same.

Thanks for the quick reply.

direction can be changed via the physics engine to my understanding. It requires manipulating just the direction of velocity (few degrees) without changing the magnitude of velocity. I will test is now to see if it feels natural.
Vector3 desiredDirection = new Vector3(0,0,1); // set this to the direction you want. (current plus few degrees random)
Vector3 newVelocity = desiredDirection.normalized * currentVelocity.magnitude;

As said this will also solve the accumulation of vector additions impacting the observed ball speed. If multiple additions add to observed speed then the player is confused why the ball speed is changing over time.

Lastly - there is no right or wrong at the end it could be a design decision but solving the boring ball case is not due to speed of the ball so I thought why do we change it, it is due to direction of the ball and slight manipulation would solve it isolation the magnitude of the velocity.

But using velocity doesn’t increase acceleration it is just a speed with a direction

Maybe I am not clear on the question - I did not say acceleration. I said speed or magnitude of velocity. In the course video if we add random numbers to velocity directly the observed speed magnitude increases or decreases also (depending on the randomized values and current direction). Instead when I write the below code in ball.cs the observed speed remains same just the ball has a slight deviation in direction which felt more natural.

private void OnCollisionEnter2D(Collision2D collision)
{
    if (hasStarted)
    {
        slightlyChangeDirection(Random.Range(0f,0.1f),Random.Range(0f,0.1f)); //randomized change
        doCollisionAudio(collision.gameObject.name); //not relevant for this case so ignore this line
    }
}

private void slightlyChangeDirection(float x, float y)
{
    float currentSpeed = myRigidbody2D.velocity.magnitude; // get current speed magnitude and isolate
    Vector2 desiredDirection = myRigidbody2D.velocity.normalized + new Vector2(x, y); //just change direction
    myRigidbody2D.velocity = desiredDirection.normalized * currentSpeed; //apply new direction with original speed
}
1 Like

In the words of “Happy Harry Hardon” from Pump up the Volume (Christian Slater) movie, “So Be It!” Just do it. You will find out quickly if it works if it is fun, or if there is an issue with doing it. Your in control it is your game. Test it, out. If it sucks then “I didn’t tell you to do it” lol… You cannot go wrong. You can always take it out.

If your using physic’s you dont have to adjust the velocity directly just (Add a force in any direction), if you want more control, mess with the velocity, you want even more control capture the vector and replace with one you have altered. Do it all, do none of it, add a wind vector to your scene, put in a vector field. There are so many solutions, ideas, approaches and none of them are wrong. It is just a question of “Can you do it with your current skill?”, “Is it fun for the game?”, “Does it make sense for your theme and feel?”

The best thing you can do is explore! Go put your Spelunking Hat on and Jump down some dark holes you may just find a light at the end of it.

If your having these thoughts and questions then your on the right track the next step really is to just dive in. Do not ask for permission that would restrict your creativity. When you implement it and it is AWESOME then you come back tell us all “I told you so!”

GOOD LUCK! :wink:

Thanks!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms