-1

Object facing left at the center of the scene, with the raycast cast with ray of transform.position, transform.forward

Object facing left at the edge of the scene with the same ray, ray still casts to the same point even though the point is being called as a direction

How could this be happening? I've already rewritten the Raycast code a few times, but here is the current iteration:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RaycastTest : MonoBehaviour {

    //width will control the distance of the two rays' direction from each other
    //range will control the collision range
    public float width, range;
    //will take width and control bias in the furture
    float b;

    void Start()
    {

    }

    void Update () {
        //loop from negative one to positive one to add left and right rays in the future
        for (int i = -1; i < 3; i += 2)
        {
            //commented line segment showing how bias will work to control the angle of the ray
            //! cant do any of that yet because this ray doesn't work
            Ray ray = new Ray(transform.position, /*(transform.forward + transform.right/2 * i) / 3*/ transform.forward);
            RaycastHit hitInfo;

            if (Physics.Raycast(ray.origin, ray.direction, out hitInfo, range))
            {
                Debug.DrawLine(ray.origin, hitInfo.point, Color.red);
            }
            else
            {
                Debug.DrawLine(ray.origin, ray.direction * range, Color.green);
            }
        }
    }
}

The issue is probably either at line 24 or 27, but I still have no clue how its coming up.

EDIT: I tested this by setting the ray direction to a constant: (-1, 0, 0) (global left) and the issue persisted, so it is probably an issue with the Ray declaration in line 24.

1 Answers1

0

I forgot to add the transform to the debug.drawline in else

Garrison Becker
  • 481
  • 3
  • 12