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


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.

| improve this answer | |
  • \$\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 '14 at 17:14
  • 1
    \$\begingroup\$ No problem. Checking the documentation is always a good first step in debugging :). \$\endgroup\$ – MichaelHouse Nov 9 '14 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.