0
\$\begingroup\$

The raycast line im rendering is acting weird, it doesn't point where I want it to.

http://i.gyazo.com/83a25c9e3118982fbd9779199d2d35be.png

I want it to point from the yellow object to the selected one but it draws it at another position.

Here is my script:

    #pragma strict

var player : Transform;
var turret : Transform;

var bullet : Transform;
var barrelEnd : Transform;

var shootRate = 0.3;

var canShoot : boolean = true;

var soundEffect : AudioClip;

function Update()
{
    shoot();
}

function shoot()
{
    turret.LookAt(player);
    var hit : RaycastHit;
    var fwd = turret.forward;
    if(Physics.Raycast(turret.position, player.position,hit, Mathf.Infinity))
    {
        if(hit.collider.transform.gameObject.tag == "Player")
        {
            if(canShoot == true)
            {
                var bulletInstance : Transform;
                bulletInstance = Instantiate(bullet, barrelEnd.position, transform.rotation);
                bulletInstance.LookAt(player.position);
                audio.PlayOneShot(soundEffect);
                w();
            }
        }
    }

    Debug.DrawRay(player.position, transform.position);
}

function w()
{
    canShoot=false;
    yield WaitForSeconds(shootRate);
    canShoot=true;
}

Thank you! I hope someone can help

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You're using the RayCast and DrawRay methods incorrectly. Both call for a direction for their second parameter, and you're passing in a position.

In the case of the RayCast you probably want your direction to be the fwd variable you create using turret.forward.

\$\endgroup\$
2
  • \$\begingroup\$ I really thought I tried that lol ,it seems like I didn't. Anyway, thank you very much for your big help! It works just the way I wanted it to work. \$\endgroup\$
    – Ssiro
    Nov 9, 2014 at 17:14
  • 1
    \$\begingroup\$ No problem. Checking the documentation is always a good first step in debugging :). \$\endgroup\$
    – House
    Nov 9, 2014 at 17:48

You must log in to answer this question.

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