- Home /
How to randomise object facing upon button down (using raycast to detect cursor over object)
This is my first time using this site, also im am quite a noob lol. Basically i am using a ray cast in conjunction with a button down to cause a primitive to change direction. i want to randomize the directions of the x and z axis upon button down over the object, but cant figure out how.
Here is what i have so far
var CubePosition : Transform;
public var theCube : GameObject;
function Start () {
}
function Update () {
var hit : RaycastHit ;
var ray:Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray,hit,100)){
Debug.DrawLine(ray.origin,hit.point);
Debug.Log("hit");
if(Input.GetButtonDown("Fire1") && hit.collider.gameObject.name == "theCube"){
/*here is where i want to randomize where my "box primitive is facing, as upon pressing the button, i would like the object to randomly face either the z+ or -, or the x + or -"
}
}
}
i just cant figure out how, and any help would be greatly appreciated, thanks in advance.
Answer by Graham-Dunnett · Jan 28, 2013 at 03:56 PM
Well the Random.Range(0,4)
function will return 0, 1, 2 or 3 randomly. So in your code you can have a switch statement that takes one of 4 paths, and rotates the game object to face on of those directions. You might use Vector3.Slerp()
for the actual rotation.
ah thats exactly the type of thing i need, could you lay it out in syntax for me if possible ? thanks so much for the response,thats definitely what i need :D