I have a 2d top down game , to which i need a script that fires a projectile in the direction om my mouse this is what i got so far
using UnityEngine;
using System.Collections;
public class Shoot : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
if( Input.GetMouseDown( 0 ) )
{
Plane zeroPlane = new Plane( Vector3.up, Vector3.zero );
Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
float distance;
if( zeroPlane.Raycast( ray, out distance ) )
{
Vector3 outputPosition = ray.origin + ray.direction * distance;
Debug.Log( "Position: " + outputPosition );
//You can use this position to rotate the bullet like so
BulletClone.transform.LookAt( outputPosition );
}
}
But it doesn’t seem to work.