0
\$\begingroup\$

I've been trying to make a weapon script which fires a projectile (prefab) straight forward.I added to the bullet the first script shown here.And the second one to the main camera.The problem is that the gun doesn't shoot straight forward.I mean it shoots straight but in small distances it looks...crap.How can I make it so it follows the hit point of the raycast.I placed my attempt below.

Script 1:

var bulletSpeed : float = 20;

function Update () 
{   
    transform.Translate(Vector3.forward * bulletSpeed * Time.deltaTime);
}

Script 2 :

var hit : RaycastHit;
    var forward = transform.TransformDirection(Vector3.forward);

    if(Physics.Raycast(transform.position, forward, hit, rayLength))
    {

    if(Input.GetButtonDown("Fire1") && canShoot==true && clip > 0)
    {
        var rocketInstance : Transform;
        rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, transform.rotation);
        rocketPrefab.LookAt(hit.point);
        w();
        gun.animation.Play("Shoot");
        audio.PlayOneShot(soundEffect);
        clip -= 1;
    }
}

Thank you for your time,please help me!

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I fixed this problem by doing this :

rocketInstance.LookAt(hit.point);

instead of this :

rocketPrefab.LookAt(hit.point);
\$\endgroup\$
1
  • \$\begingroup\$ Yep that sounds about right. \$\endgroup\$
    – Alex
    Commented Jul 25, 2014 at 5:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .