- Home /
Creating 4 objects going in all 4 directions?
So I'm working on the script for my game and for the game im trying to make it to were you control an "Exploder" and you press f and it creates 4 explosion balls so to say. But i have no clue on how to make them go different directions. Do I just rotate the Empty object or is there a Vector3 command to make it go left.. Heres my script so far
var fireballprefab: Transform; var mainball: Transform; var fireballspeed = 2000; var fireballprefab2: Transform; var fireballprefab3: Transform; var fireballprefab4: Transform; var firebutton = "f";
function Update () {
if(Input.GetKeyDown(firebutton))
{
var fireball = Instantiate(fireballprefab, gameObject.Find("ballcreation").transform.position, Quaternion.identity);
fireball.rigidbody.AddForce(transform.forward * fireballspeed);
var fireball2 = Instantiate(fireballprefab2, gameObject.Find("ballcreation2").transform.position, Quaternion.identity);
fireball2.rigidbody.AddForce(transform.forward * fireballspeed);
var fireball3 = Instantiate(fireballprefab3, gameObject.Find("ballcreation3").transform.position, Quaternion.identity);
fireball3.rigidbody.AddForce(transform.right * fireballspeed);
var fireball4 = Instantiate(fireballprefab4, gameObject.Find("ballcreation4").transform.position, Quaternion.identity);
fireball4.rigidbody.AddForce(transform.left * fireballspeed);
}
}
Answer by owenpat · Feb 26, 2011 at 04:55 AM
Turns out that left is: transform.right*-1
or just -transform.right
. Likewise, backwards is -transform.forward
If you want to mess with other directions, you can add whatever you like and then use Normalize to chop it down to length 1. About 30 degrees forwards right is: ((transform.forward*2+transform.right).normalized)*speed
.
Vector3.right
, and so on, work the same way, except they give world movement: East, north ...
Working in a similar manner, .left .down and .back direction exist, though not listed on the scripting reference.
Ah. I was looking at transform.left
in the code above and thinking that it wouldn't compile. I guess his real mistake was forgetting to change the 2nd forward to backward.