I’m trying to implement a cover system where you can sort of bind yourself to objects by pressing space, where the character will then Lerp to the object if it’s within a certain range. The character then should only be able to move along the one axis of that object until he comes to the edge. I’ve gotten so far as to be able to get the player to lerp to the object, but I’m not quite sure how to make him “stick” to the cover or walk in a restricted manner along it. None of the cover objects I’ve used so far have anything but flat faces for now to keep it simpler. I searched a bit on the internet, but most of the solutions I found were either very vague or geared towards third person characters, which this is not. The character is first person, which is what’s also proving to make it slightly more difficult. Here’s the code I have so far to have the character lerp and take cover. So far it’s only up to the point where the character lerps, and the inCover boolean is switched to true.
It’s not all the code, but it’s what I consider to be most relevant to this. Honestly, the take cover script right now has almost nothing dealing with the restricted movement because I haven’t much of a clue where to even start with that.
function Update()
{
var hit: RaycastHit;
if (Physics.Raycast(transform.position - Vector3(0, 0.5, 0), transform.forward, hit))
{
distance = hit.distance;
if (distance <= 5)
{
if (hit.transform.tag == "CoverCrouch")
{
if (hidden == true)
{
showGUI();
}
if (Input.GetKeyDown(KeyCode.Space))
{
startLerping();
inCover = true;
}
}
Then here is the movement script I have (both are attached to the player). I also want to be able to look in a restricted viewing range with the characters back against the wall with a mechanism that I’ll implement after I get this down to peek over and fire over the cover. That’s not too important in the context of this question but I’m including it for context.
function Update()
{
//handles movement forward and back and strafing
if (chController.isGrounded)
{
var moveDirection: Vector3 = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.rotation * moveDirection;
moveDirection *= speed;
}
moveDirection.y -= gravity * Time.deltaTime;
chController.Move(moveDirection *Time.deltaTime);
//sprinting
if (Input.GetKey(KeyCode.LeftShift))
{
speed = sprintSpeed;
} else if (!Input.GetKey(KeyCode.LeftShift))
{
speed = walkSpeed;
}
//handles look rotation
yRotation += Input.GetAxis("Mouse X") * horizontalSensitivity;
xRotation -= Input.GetAxis("Mouse Y") * verticalSensitivity;
//stops player from viewing greater than 90 degrees
xRotation = Mathf.Clamp (xRotation, -90, 90);
//handles look smoothing
currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, xRotationV, lookSmooth);
currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, yRotationV, lookSmooth);
theCamera.transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
transform.rotation = Quaternion.Euler(0, currentYRotation, 0);
}
Thanks for any help you guys can give. I know it’s a bit open-ended as of right now, but my concept of how to go about doing this right now is very hazy at best so any information is appreciated.