Hi,
What i want is that when my player walks within a certain range of a wall they are able to “lock” to it and use it as cover, this enables the player to have cover and shoot
So far: Walk within a range of wall, press shit, changes camera (to third person camera), then if shift is pressed again it reverts back again
var distance : float = 5;
var FPSCamera :Camera;
var thirdPersonCamera: Camera;
var cameraSwitched : boolean;
function Update ()
{
var dir = transform.TransformDirection(Vector3.forward);
var hit: RaycastHit;
Debug.DrawRay(transform.position, dir * distance, Color.blue);
if (Physics.Raycast(transform.position, dir, hit, distance))
{
if (hit.collider.gameObject.tag == "coverWall")
{
Debug.Log("can cover");
if (Input.GetKeyDown(KeyCode.LeftShift))
{
if(cameraSwitched)
{
cameraSwitched = false;
}
else if(!cameraSwitched)
{
cameraSwitched = true;
}
}
}
}
if (cameraSwitched == true)
{
FPSCamera.enabled = false;
thirdPersonCamera.enabled = true;
}
if (cameraSwitched == false)
{
FPSCamera.enabled = true;
thirdPersonCamera.enabled = false;
}
}
Essentailly my question is- How do i make it so when the character engages with the cover wall he can only walk to the left or right of the wall?
Also how do i make it so that this only happens when the player holds the shift button instead of just pressing the button?
The issue with the shift key is quite easily solved because there is a corresponding GetKeyUp function - just enable cameraSwitched if the key is down and disable it if the key is up.
Are the walls just arbitrary buildings and other objects in the scene or do they have a particular layout? (e.g., square grid, all walls lie along one axis, etc) In the general case you will probably need to examine the wall mesh and prevent the player moving more than a small distance away from the nearest point. However, if the walls have a more regular layout then you might get away with something a bit simpler than this.
ok thank you (sorry for the late reply my internet dropped out when i was posting this so i ddin’t notice when it didn’t work)
The GetKeyUp is working perfectly
the walls are essentailly arbitrary when simply put
My aim for the cover walls is to have them in any shape, form, and direction - the only thing that makes a “cover” wall that is by tag
Did you ever figure this out? I’m looking into something similar. I have a stealth system that has the character press against the wall, however, I use a collision volume to detect what areas allow this, and while the char turns ok, their position doesn’t lock to the wall, they seem to be a bit out from it.
I assume this has to do with the character controller, but I don’t know how to fix it. I’d also like to have it so that when the player reaches the ends of the wall, they look around, but don’t leave cover.
If you’ve ever played Tenchu, this is the system I’m trying to make.
Still having trouble pinning the player to the wall. The character controller volume seems to prevent the player from touching the wall with their back.
I’m using a Raycast to detect distance to the wall’s collider, then playing a turn animation followed by a wall stand animation.
Ideally, I’d like the player to touch the wall during the turn, and hold that position during the stand.
I did by lerping the players position to the -(hit.normal) *0.001f of the wall itself. It moves the player toward the wall. I have to look at my code again.
I used sphere cast or overlap sphere to get an omni direction of a wall, and attach my char to it, so if he rotates it doesn’t screw up like a raycast would. My solution is not perfect.
I’ve solved the distance (for now) by reducing the size of characterController.radius while the character is at the wall.
I don’t know if this is ideal, but it seems to work ok for now.
I’ve got a cube object for testing, and I need the player’s back flush with the wall on each side, could you share your rotation solution, as this doesn’t seem to be working.
The animation will start out correct, but the player will still be able to rotate away from the wall.
After he locks to the wall, I tried restricting y rotation
if (TPAnimator.Instance.State == TPAnimator.CharacterState.PressedWallStand)
{
if (animation.IsPlaying("P1WallHideStand"))
{
transform.rotation = Quaternion.Euler(transform.eulerAngles.x,
0,
transform.eulerAngles.z);
}
}
the above example, 0, only allows the character to face one way, with no y rotation.
It’s not related to the cube sides, it’ll work facing one side, but will be sideway or backwards on the others.
I’ve got a feeling there’s some way to modify this code to force that y component to be parallel with cube sides, but I don’t know how to get that angle form the cube.
if (TPAnimator.Instance.State == TPAnimator.CharacterState.PressedWallStand)
{
if (animation.IsPlaying("P1WallHideStand"))
{
transform.rotation = Quaternion.FromToRotation(-transform.forward, TPAnimator.Instance.hit.normal) * transform.rotation;
}
}
But Im not sure how to stop the player from sneaking past the end of the wall(cube face).
Is there some way to get the length of the wall from the raycast hit? I could then probably limit movement while standing at the wall to some value of just less than that length.
What you could do is just cast a ray towards the wall each frame while the character is in cover and if the ray does not find a wall behind the character, move him to the last valid position that is stored every time the ray successfully hits the wall
I’d also prefer to have a ‘look around corner’ animation as he approaches the end of the wall.
Perhaps the only way to do that is add custom trigger volumes at the end of each wall, and stop movement and go to the ‘look around’ motion as he enters.
I would have tried something funky and made an editor script that projected the model vertex data onto a plane, exclude vertices that are not-edge vertices to get a “ground footprint” of the mesh. Using this list of edge vertices you can then make an animation curve that traces the outside of the footprint at what ever offset you feel is appropriate to accommodate your models. You can also compute the angle between adjacent vertices to sense corners or sharp bends and place animation events to trigger head pop out actions. At run / placement time you might have this recomputed to take into account terrain contours. Use the editor script to save this and some other odd data (more later) in a script you’ll place on the object.
Armed with this curve a trigger script can then sense player proximity and toggle movement and rotation along a set path. My hack version would include a distance check on a few curve test keys to see if I was close enough to the track to get on. To get onto the rail curve find the nearest point (saved from the ontrigger stuff we did earlier) set the follow point to lerp from the units position to the curve until you’re close enough to be considered on. I might even trigger a “put your back to the wall” animation when the unit arrived. While on the track I’d use the forward or back key to manually get on or off (relative to the center of the footprint which I had the foresight to save from the editor script along with the curve). Movement using the left or right motion would set my “follow point” some where along the curve using the relative direction faced. evaluating the curve using + or - current position depending on which way the player wants to go. Use the animation event triggers to additively play your head pop animation before continuing along the curve.
Modify your existing movement controller such that it has a follow the follow point mode … look at follow point… transform.lerp… of course some of the animations you would want to do would be specific to the model and possibly non-existent on others, so implementing functions locally that will handle events such as ArrivedAtWall, MovingAlongWall, HeadPeepingAtCorner can sensibly implement any animations and user interaction events as you need them.
Another random idea is a camera zoom or swivel function that places the camera in such a way that it’s clear that there’s a transition taking place to the player and is a clear indicator if they’re on the wall,off the wall or transitioning to or from the wall.
If you do write such a beast I’d love to see how it turns out.