- Home /
Ray is not rotating with Object using Physics.Raycast
Hello, So I'm having trouble trying to rotate my ray with my player object. I've looked on the forums and tried out the answers for similar questions but nothing seems to have worked for me and I'm not sure what it is I'm doing wrong. Basically I've cast a ray from my player object to detect when it hits a cube. Once it does, I want my player object to rotate a certain angle and the ray to rotate with it. Here is my code:
function PlayerCubeInteraction()
{
var hit: RaycastHit;
var newRay = new Ray(transform.position, transform.forward);
if (Physics.Raycast(newRay, hit, raylength))
{
if (hit.collider.tag == "cubeDown")
{
transform.Rotate(0, 10, 0, Space.Self);
GetComponent.<Rigidbody>().velocity = Vector3 (0,0,0);
Debug.Log("collision detected");
GetComponent.<Rigidbody>().AddForce(0, 0, -speed);
}
}
}
This function works in my FixedUpdate() function.
Answer by Mick Fuschi · Jul 05, 2015 at 04:37 PM
Don't get the problem, I tried your code and it works. Maybe use Debug.DrawRay(transform.position, transform.forward * raylength) to debug your problem.
I tried this code with two simple cubes, one as a player and another as the "cubeDown"-tagged one.
Hope you'll find your problem with Debug.DrawRay.
Thanks for the help! I had Vector3.left in my Debug.DrawRay rather than transform.forward but it's working now!