- Home /
Bullet always facing the sky.
Ok people, once again i'm here bagging on my knees for your help. So here's the deal. I have this Tank that is supposed to shoot missiles and here's what i have.
@script ExecuteInEditMode()
var bulletPrefab: Transform ;
var tirosDisparados: int =0;
var bulletSpeed: float = 60;
var buttonX: int = 0;
var buttonY: int = 0;
function Update()
{
if (Input.GetButtonDown("Fire1"))
{
var shoot = Instantiate(bulletPrefab,GameObject.Find("Spawn").transform.position,Quaternion.identity);
shoot.rigidbody.AddForce(GameObject.Find("Spawn").transform.forward * bulletSpeed);
//Bullet Counting
tirosDisparados = tirosDisparados + 1;
Debug.Log(tirosDisparados);
}
}
The problem is, no matter the rotation of the prefab, my missile always faces to the sky, I believe it's something related to the quaternion, but since, i don't know how to work with it (i'm a noob), i'm asking for your help... Hope you can help me. Thanks once again to this wounderfull community.
Answer by fafase · Jul 10, 2012 at 07:05 PM
Quaternion.identity is short for no rotation.
Try this one:
var spawn:GameObject;
//This is creating a reference to the spawn object for faster access.
function Start(){
spawn =GameObject.Find("Spawn");}
var shoot =Instantiate(bulletPrefab,spawn.transform.position, spawn.transform.rotation);
shoot.rigidbody.AddForce(spawn.transform.forward * bulletSpeed);
Thanks, it didn't work but the idea of finding the object on the start was a very good hint, thanks.
Answer by nventimiglia · Jul 10, 2012 at 07:09 PM
You are using Quaternion.identity (zero)
if (Input.GetButtonDown("Fire1"))
{
var spawn = GameObject.Find('Spawn');
var shoot = Instantiate(bulletPrefab, spawn.transform.position, spawn.transform.rotation);
shoot.rigidbody.AddForce(spawn .transform.forward * bulletSpeed);
//Bullet Counting
tirosDisparados = tirosDisparados + 1;
Debug.Log(tirosDisparados);
}
Another thing, you really shouldent Find the gameobjects like that. Its best you cache the spawn object on awake.
That didn't solve my problem but you were right about spawing objects on the go, anyway, for now, i made it to shoot balls so there is no problem after all... I'll check on this later...
Answer by Satamanster · Jul 10, 2012 at 08:45 PM
For fafase and nventimiglia :
Using the spawn rotation fixes it... Or not, well, if i rotate the spawn now, x axis makes my missile being shoot upwards or downwards, the y one, makes it shoot more to the left or to the right, and only z makes the bullet rotate, in this case the wrong direction, since now instead of being always facing up, is always facing left, as i set the spawn z rotation to 90...