Hi all, im quite new to Unity and am working on a simple shooter on rails, however I am having trouble getting the projectiles when you shott to head out in the right direction.
I have made a gun object that is attatched to the camera, I need the gun object to shoot out wherever the player clicks, im not sure if this is the best way to do it or if I should maybe use raycasting instead?.
Here is the code I have so far, any help would be greatly appreciated.
#pragma strict
var projectile : Rigidbody;
var speed = 20;
var mousex = 0;
var mousey = 0;
var gun : GameObject;
function Start () {
}
function Update () {
mousex = Input.mousePosition.x/2;
mousey = Input.mousePosition.y/2;
gun.transform.LookAt(Vector3(mousex,mousey,0));
if (Input.GetButtonDown("Fire1"))
{
var instantiatedProjectile : Rigidbody = Instantiate( projectile, transform.position, transform.rotation);
instantiatedProjectile.velocity = transform.TransformDirection( Vector3(0,0, speed));
Physics.IgnoreCollision( instantiatedProjectile. collider,transform.root.collider);
}
}