I am having an odd issue, I tried to use classic game programming code to maintain my ball velocity after tweeaking, like thus:
get velocity magnitude, save as “speed”
tweak velocity
normalize velocity
scale velocity back up by multiplying with speed
…but my ball takes off at lightspeed. I commented out the tweak, so theoretically it should just save magnitude as speed, normalize, then remultiply by speed back to original magnitude, but it STILL goes nuts. Why?
Here’s my actual code:
private void OnCollisionEnter2D(Collision2D collision)
{
if (hasStarted)
{
AudioClip clip = ballSounds[Random.Range(0, ballSounds.Length)];
myAudioSource.PlayOneShot(clip);
Vector2 velocityTweak = new Vector2(Random.Range(0f, randomFactor),
Random.Range(0f, randomFactor));
float speed = myRigidBody2D.velocity.magnitude;
myRigidBody2D.velocity.Normalize();
//myRigidBody2D.velocity += velocityTweak;
// myRigidBody2D.velocity.Normalize();
myRigidBody2D.velocity *= speed;
}
}