- Home /
can anyone find the issue with this code?
this code is called as the player is wall running, it turns the player as the normal they are attached too changes for use on round surfaces. the issue is /not/ in the "_left" portion, that works entirely as expected. the issue is when the player tries to run around a curved surface on the right side. the player does not rotate, aside from the initial rotation on impact with the surface which uses the same exact rotation code, the difference being that this is in update so it happens for the duration of the wallrun.
if (_wallRunning)
{
if (_left)
{
Debug.Log("left");
RaycastHit hit;
Ray ray = new Ray(transform.position,-transform.right);
if (Physics.Raycast(ray, out hit))
{
transform.rotation = Quaternion.FromToRotation(Vector3.right, hit.normal);
}
}
else if (_right)
{
Debug.Log("right");
RaycastHit hit;
Ray ray = new Ray(transform.position, transform.right);
Debug.DrawRay(transform.position, transform.right, Color.red); //draws ray being cast
Debug.Log(Physics.Raycast(ray, out hit));//returns true
if (Physics.Raycast(ray, out hit))
{
Debug.Log(transform.rotation);//check rotation
transform.rotation = Quaternion.FromToRotation(-Vector3.right, hit.normal);
Debug.Log(transform.rotation);//no change
}
}
}
Here is a link to a (very bad) video showing the problem on youtube.
also: this has my current hierarchy and the script attached to the player
Answer by Bunny83 · Apr 25, 2017 at 08:59 AM
Well, have you made sure that:
"_right" is true all the time while you wall run? Where and how is it set to true? In short: are you sure your code actually runs every frame?
You should limit your raycast length. Otherwise you could "wallrun" a wall that's 100m away to your right / left
Are you sure the object your raycast hits is actually the wall? Maybe you hit something on your player?
When using Debug.Log you should always add some description to be able to identify which log comes from which line in code.
Debug.Log("R object hit: " + hit.gameObject.name);
Debug.DrawRay(hit.point, hit.normal, Color.yellow); // vizualize hit point and hit normal
Debug.Log("R old rotation: " + transform.eulerAngles);
transform.rotation = Quaternion.FromToRotation(-Vector3.right, hit.normal);
Debug.Log("R new rotation: " + transform.eulerAngles);
Note that i prefixed the messages with "R" so we know those come from the "right" part and not the "left". Seeing what you actually hit and where should help finding out what's wrong.
okay, so the issuw was that the raycast appeared to be hitting the wall, but it was getting the hit information from a trigger i had on the player ins$$anonymous$$d. thank you for pointing me in the direction i needed to fix this issue.
I fixed this issue by going to:
Edit > Project Settings > Physics > Uncheck "Queries Hit Triggers"
Answer by tanoshimi · Apr 24, 2017 at 07:13 PM
Trying to understand your logic, but on lines 10 and 23, did you not mean:
transform.rotation = Quaternion.FromToRotation(transform.right, hit.normal);
and
transform.rotation = Quaternion.FromToRotation(-transform.right, hit.normal);
?
at first i thought the same thing, however upon changing Vector3 to transform, it causes the player to be angled to the world.right ins$$anonymous$$d of the local.right for some reason.
That sounds odd. Your objext isn't parented to another object which has been rotated, has it?
nope, this is directly attached to the player which isnt a child of anything. This is a video link to youtube to show you exactly what is happening with the code above. please take note that when i jump to the walls on the right side, everything snaps the players rotation as it should, until it needs to rotate the player on the right side curve.