Hi, I’m trying to get it so my bullet moves along the raycast when i press Fire1 in this javascript. Its attached to my main camera which is a child of my third person character. A couple of things, 1) Nothing actually happens when i press fire1, i’m expecting to see my bullet travel along the raycast, 2) does it have to be a rigidbody or a transform for the bullet to appear? Its giving me the NullReferenceException: Object reference not set to an instance of an object at line 10 and 12, is this my problem? - and can someone explain what that error means?
#pragma strict
var projectile : Rigidbody;
var speed = 20;
function Update () {
if (Input.GetButtonDown("Fire1"))
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5));
if (Physics.Raycast (ray, hit))
{
Debug.DrawLine(transform.position, hit.point, Color.red);
Debug.Log(hit.point);
var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation);
instantiatedProjectile.velocity = (hit.point - transform.position).normalized * speed;
instantiatedProjectile.rotation = Quaternion.LookRotation(instantiatedProjectile.velocity);
}
}
}