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


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()

function shoot()
    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);

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

function w()
    yield WaitForSeconds(shootRate);

Thank you! I hope someone can help


1 Answer 1


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.

  • \$\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
    Commented Nov 9, 2014 at 17:14
  • 1
    \$\begingroup\$ No problem. Checking the documentation is always a good first step in debugging :). \$\endgroup\$
    – House
    Commented 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 .