Hello,
What I’m trying to do is make a sort of Pathing algorithm with using forward/diagonal rays.
Right now my code is : (OLD CODE, working code below)
void managePathingAndMovement()
{
forwardRayCast();
if (!moveTowardsEnd)
{
translate();
}
}
private Vector3 moveDirection; //No point in using atm
void forwardRayCast()
{
Ray fowRay = new Ray(_rayCastObject.position, Vector3.forward);
RaycastHit fowHit;
if (Physics.Raycast(fowRay, out fowHit, Mathf.Infinity)) //Change Mathf.Inf
{
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y + 1, 0); //Sets the y rotation to +1 every frame while stil Raycasting
}
else
{
moveTowardsEnd = true;
}
}
void translate()
{
transform.Translate(Vector3.forward * 10 * Time.deltaTime);
}
I can’t actually rotate the transform itself, and the weird thing is that my Vector3.forward changes as my character rotates, but the Raycast seems to be working independently.
Also, I’ve tried shooting the ray from both the transform itself and the child object. Both times it’s just pointing dead ahead. How would I go about fixing this? – Fixed with multiplication ![]()
private Vector3 moveDirection;
void forwardRayCast()
{
moveTowardsEnd = false;
Vector3 forward = Quaternion.Euler(_rayCastObject.localEulerAngles) * Vector3.forward; //Rotates toward the localRotation by using eulerAngles
Ray fowRay = new Ray(_rayCastObject.position,forward );
RaycastHit fowHit;
Debug.DrawRay(_rayCastObject.position, forward);
if (Physics.Raycast(fowRay, out fowHit, Mathf.Infinity)) //Change Mathf.Inf
{
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y + 1, 0); //Sets the y rotation to +1 every frame while stil Raycasting
_rayCastObject.localEulerAngles = transform.localEulerAngles;
}
else
{
//moveTowardsEnd = true;
}
}
Thanks for the response. I literally just got done doing some multiplication (after adding the vectors for a few hours) and I came up with this: Vector3 forward = Quaternion.Euler(_rayCastObject.localEulerAngles) * Vector3.forward; What that does is it keeps my "local" forward the same based on the character's rotation. To be sure of this, I also set the eulerAngles from the transform and the rayObject(transform) equal to each other. The only time it seems I come to an answer is after I spent hours trying to come up with it, and always after someone posts one lol
– LairinusI am glad that you find a way around your problem, just to inform you Vector3 forward = Quaternion.Euler(_rayCastObject.localEulerAngles) * Vector3.forward; the computed
– Chronos-Lforwardis equivalent totransform.forward....... derp on my part lol. thanks for pointing that out.
– Lairinus