I am beginner and 3d artist! I making Space Shooter game in JavaScript with camera from bird-view, spaceship can turn around with “AddForce” and i have problem with intantiation bullets from spawner(empty game object) that i created for shooting! When i turn around my SpaceShip spawner dont rotate along ship so i can shoot straight only in one world z direction… how can i make him to shoot where my spaceship look at?
var speed : float = 10;
var rotationSpeed: float = 10;
var bullet : GameObject;
var bulletSpawn : Transform;
var sideForce : float = 20;
var topSpeed : float = 20;
function Update()
{
//Controls for ship...
if(Input.GetKey("s") || Input.GetKey("0"))
{
rigidbody.AddRelativeForce(Vector3.back * speed * Time.deltaTime);
}
if(Input.GetKey("w") || Input.GetKey("1"))
{
rigidbody.AddRelativeForce(Vector3.forward * speed * Time.deltaTime);
}
if(Input.GetKey("q") || Input.GetKey("0"))
{
rigidbody.AddRelativeForce(Vector3.left * sideForce * Time.deltaTime);
}
if(Input.GetKey("e") || Input.GetKey("0"))
{
rigidbody.AddRelativeForce(Vector3.right * sideForce * Time.deltaTime);
}
if(Input.GetKey("a"))
{
transform.Rotate(Vector3.down * rotationSpeed * Time.deltaTime);
}
if(Input.GetKey("d"))
{
transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
}
//SHOOTING...
if (Input.GetKey("f"))
{
Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation);
}
//unsuccessful attempts to make braking sistem on spaceship (speed slowing down)
if (rigidbody.velocity.magnitude > topSpeed)
rigidbody.velocity = rigidbody.velocity.normalized * topSpeed;
transform.eulerAngles.x=0;
transform.eulerAngles.z=0;
transform.position.y=0;
if(Input.GetKeyDown(KeyCode.LeftShift))
{
rigidbody.velocity = Vector3.zero;
}
}