- Home /
clamping player position while in cover
So, I'm trying to integrate a cover system, and so far i have it to the point where the player can lerp to the object, and walk along it's surface. But, I'm running into issues right now, because if once the player walks along the object, they can just keep walking beyond the edges of the cover object, which is, needless to say sort of a problem. I tried clamping the x and z positions of the player, creating an effective window that they can sort of walk in, and because the direction of movement is also limited, it seemed a fair attempt. But, when they hit the edge of that window, they slide along it like a collider I think, which causes the player to then be pushed into the cover object, but then my script resets the position to avoid having the player be displaced. That creates a very noticeable jitter at the edges of that walkable window. Secondly: I'm not sure how to detect the edges of a cover object, and I don't want to manually have to enter dimensions for every single object face that the player could ever want to take cover on. Here's what I have so far, and the problem right now lies in those last few lines of code about limiting the character to walking only behind the cover. Any help improving this code would be much appreciated.
function restrictMovement()
{
//establishes normal reference to be applied to object later
var normal : Vector3 = new Vector3 (0, 1, 0);
//establishes tangnt vector based on the normal reference vector and the transform of the object
var tangent : Vector3 = Vector3.Cross(normal, coverObject.transform.forward);
//ensures no errors are acquired if the magnitude is 0
if (tangent.magnitude == 0)
{
tangent = Vector3.Cross(normal, Vector3.up);
}
//can only move after lerp is done
yield WaitForSeconds(lerpTime + 0.1);
//adjust movement to respond to the player differently
fpsController.moveDirection =-Input.GetAxis("Horizontal") * tangent;
fpsController.moveDirection = fpsController.moveDirection * fpsController.speed;
//clamps player movement to size of object
transform.position.x = Mathf.Clamp(transform.position.x, coverObject.transform.position.x - 3, coverObject.transform.position.x+3);
transform.position.z = Mathf.Clamp(transform.position.z, coverObject.transform.position.z-3,coverObject.transform.position.z + 3);
}
Ok, so I replaced the last two lines of code, and I like this better just because I can visualize what my own code is saying a little easier, but it still has heavy jitter at the edge of the walk-able area, which I can't seem to fix. Here's the replaced code:
relPosition = transform.position - coverObject.transform.position;
transform.position = coverObject.transform.position + Vector3.Clamp$$anonymous$$agnitude(relPosition, 4);
Your answer
Follow this Question
Related Questions
How Can I Stop Rotation From This Script? 0 Answers
Clamping "X" Position Movement On Platform (Code Inside) 2 Answers
Array.Push() for Vector3[] or how to add items to Vector3 array without knowing index 1 Answer
How to tell if two blocks are right next to each other?(2D) 1 Answer
2D get touch input 1 Answer