Randomizing transform.rotation...

Hey guys, I’m sure this is simple but I’m not getting it, damn…

…I’d like to randomize the rotation of the coordinates a projectile comes out of.

(I made the code a quote btw so I could bold the section I’m referring to. :slight_smile: )

I can add a Vector3 to my transform.position, and I’d like to do the same to transform.rotation, randomizing the X and Y.

The idea being, if not evident, is to have a slightly random trajectory of the projectile so it isn’t identical each and every time.

Thanks in advance as I hang my head in shame for not figuring it out.

-Steve

This should do the trick Untested

transform.rotation + Quaternion.Euler (Random.Range (-10.0, 10.0), Random.Range (-10.0, 10.0), 0);

Funny enough that was my very last idea before posting… :smile:

…unfortunately:

Thanks though! Quick response :smile:

Use Random.rotation.

–Eric

Fixed the code above was a simply typo most likely :smile:

= not +

    transform.rotation = Quaternion.Euler(Random.Range(-10.0F, 10.0F), Random.Range(-10.0F, 10.0F), 0);

Edit*

Had tominy posts open and did not get to this one in past 5mins so did not see someone already posted.

Post above mine works too and is more clean :wink:

Made shiney and in a codebracket.

    transform.rotation = Random.rotation;

Actually I think Random.rotation wouldn’t actually work very well after all in this case, after reading the first post again and understanding a little better this time. :wink:

–Eric

@Eric: Yea exactly…need that range… :smile:

@ackyth: Still doesn’t work :frowning:

Here’s the code with your addition:

var instProj : Rigidbody = Instantiate (weapProjectile, transform.position + Vector3 (0,0,0), 
			 transform.rotation = Quaternion.Euler(Random.Range(-10.0F, 10.0F), Random.Range(-10.0F, 10.0F), 0));

Bitches about things such as:

Gah.

I really am appreciating the effort though guys. :slight_smile:

-Steve

@SteveB: Replace my + with a *

Okay two things:

First off Daniel is the winner, but I’d like to understand why it was multiply rather than addition…not the most versed in quaternion math…I’m glad I got as far as what Daniel originally suggested, only I’d never think to swap out + for *.

Second, I was applying a rotation to the instanced object, but not its vector…I’m a moron so this post is more or less academic, as I was able to instantly figure out how to make the random vector work…

Sorry but thank you SO much guys!

:sweat_smile:

-Steve

Well, it’s not really either addition or multiplication - those terms are kind of meaningless for quaternion math. They just (somewhat arbitrarily) chose * to represent the operation “rotate a quaternion by another quaternion”. (Perhaps it’s an industry standard, in which case “they” refers to someone who came before UT - either way, it was just arbitrary.)

Ah shit so it would be even LESS likely that I would’ve come to this solution haha, sheesh…

…wow thanks man, interesting stuff. :slight_smile:

-Steve