Jump to content

View more

Image of the Day

The night is still, but the invasion brings chaos. #screenshotsaturday #hanako #indiegame #gameart #ue4 #samurai https://t.co/cgILXuokoS
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Why to normalize Vector2.one in Unity?

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
1 reply to this topic

#1 funnymango   Members   

106
Like
0Likes
Like

Posted 04 September 2014 - 03:17 AM

A question about this code in a unity pong tutorial:

// Give the ball some initial movement direction
rigidbody2D.velocity = Vector2.one.normalized * speed;

 

Why do we have to normalize the Vector2.one vector if its already one?

 

Thanks



#2 haegarr   Members   

7360
Like
3Likes
Like

Posted 04 September 2014 - 03:25 AM

Due to the documentation, Vector2.one is the vector (1,1) which has the length sqrt(1^2 + 1^2) = 1.4142 approx., so it is not of unit length.

 

EDIT. For the case you do not know: The normalization ensures that the length of the vector is 1, not that all of its components are 1. So, in this case, the result of normalization would be approximately

    (1,1) / length( (1,1) ) = (1,1) / 1.41412 = (0,707,0.707)

 

This step ensures that the velocity vector has a length equal to the value of speed. Otherwise its length would depend on the actual direction, so you would have higher speed in diagonal directions than in principal directions.


Edited by haegarr, 04 September 2014 - 03:35 AM.





Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.