- Home /
how to use random.range to switch switch facing of a rigidbody between z+,- and x+,-
wondering how to use random.Range , to switch the facing of an object between z+ or - and x+ or - . i know that i will also have to use Vector3. i know i will also have to use addtorque as the object i want to apply this translation to is a rigid body. so any input you guys could give me would be amazing, as i just cant get my head around this, thank you in advance!
The context in which i wish to use this script , is a cube primitive, which if clicked, will randomly face a different direction . it already has a force applied in the forward axis (z+)
Are you trying to turn the cube so that it drives in a new direction, or is the cube turning independent of the force you've already applied?
Answer by Loius · Jan 28, 2013 at 08:22 PM
If you want it to just be directly along one of the axes, you'd pick a random number (from four) and use that to determine which direction:
var dir : int = Random.Range(0,4);
var tx : Transform = cube.transform;
switch( dir ) {
case 0: tx.forward = Vector3.right; break;
case 1: tx.forward =-Vector3.right; break;
case 2: tx.forward = Vector3.forward; break;
case 3: tx.forward =-Vector3.forward; break;
}
If you want any facing at all along the X/Z plane:
var newDir : Vector3 = Random.insideUnitSphere;
newDir.y = 0;
cube.transform.forward = newDir;
Your answer
Follow this Question
Related Questions
Why isn't AddTorque working on this character? 0 Answers
AddTorque doesn't rotate? 3 Answers
Rigidbody.AddTorque/AddForce Question. 1 Answer
Character - Collider With Scene 0 Answers
Rigidbody.MovePosition relativeTo parent 0 Answers