Raycast facing the wrong direction

Hi guys,
Im trying to draw a raycast directly out in front of the player I am instantiating. This was 100% functioning correctly until I changed my player controller to work with an Xbox controller instead of the mouse and keyboard. Now instead of the Raycast being projected straight out in front of the player, it often gets projected at different angles. I confirmed this by using Debug.DrawLine(transform.position, forwardVector). If I leave my game scene run, the way the raycast is facing constantly changes as if both it and the player are rotating at different speeds. At some moment the raycast will face straight out, but a while later it may be off at 90 degrees to the player.

Does anyone know what caused this issue or why this happens? Could it be an issue with deadzones?

This is the script that is attached to the Player.

private int damage;
    private float distanceBetween;
    private int rayLength = 5;

    // Update is called once per frame
    void Update () {
        /*If the fireButton is pressed,a raycast is sent out front the front of the player. The raycast is like an invisible
         * straight line. If this line hits an object, (ie if something is in front of the player), it then checks to
         * see what that object is. It does this by checking the gameobjects tag.
         */
        if(Input.GetButtonDown("Fire1"))
        {
            Debug.Log("Inside fire loop");
            RaycastHit hit;
            Vector3 fwd = transform.TransformDirection(Vector3.forward);
            damage = Random.Range(0,50);
            Debug.DrawRay(transform.position, fwd);

            if(Physics.Raycast(transform.position, fwd,out hit, rayLength))
            {
                /*If the raycast hits a tree, the player is within range of the tree. The damage value is then
                 * sent to the ApplyDamage function which is in a scipt attached to the tree.
                 */
                if(hit.collider.gameObject.tag == "tree")
                {
                    Debug.Log("Chopping tree: " + damage);
                    hit.transform.SendMessage("ApplyDamage", damage,SendMessageOptions.DontRequireReceiver);
                }
                else{
                    Debug.Log("Not chopping a tree");
                }
            }
        }
    }

Well, you could try this on line 15:

transform.forward

That is in local coordinates, so it shouldn’t mess up. I have had weird things happen with transformDirection before, and this usually fixes it. In fact, I recommend only using this if you need to generate a movement vector based on a Vector3 made like this:

Vector3(<InputAxisX>, 0, <InputAxisY>);

Also, I wouldn’t recommend using SendMessage too often, as it apparently brings down performance. Since it appears you just use it for damage calculation, it should be fine.

Hey thanks for the fast reply. I actually tried that already, and it gave the same issue. I’m really beginning to think that it may have something to do with deadzones as I wasnt getting this issue when I was only using mouse and keyboard. Its a strange one thats causing me a lot of headache. Thanks for the suggestion though.

Your suggestion to change the code to “transform.forward” seems to always draw the raycast facing directly behind the player. This is an improvement from the code I had that drew it in ever changing directions. So I just changed Vector3 fwd = transform.forward;
to
Vector3 fwd = -transform.forward;

I hope this doesnt cause issues down the line. Thanks again for your help.

I don’t think it’s deadzones… :smile:

I had this problem with a skybox sphere that auto-adjusts the up direction to the player, and originally using transformDirection made the sky spin out of control… Got dizzying after a while! :wink: (Special case of planet gravity, ye see? :smile:)

Heh… that one always works with reversed directions! :slight_smile: