• FEATURED
• FEATURED
• FEATURED
• FEATURED
• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Stealth system

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1steviebops  Members

Posted 08 May 2013 - 01:41 PM

I've been working on a Tenchu-style stealth system, and I've run into a problem.
I use Unity, and asked over there, but I'm not getting a solution.

My problem is when the player presses their back against a wall, in this case a cube. I can only get the player to face (away) parallel with one side of the cube, by using the camera, on the other sides, I can't figure out how to do it.
Has anyone here a solution please?

var dir = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position, dir * distanceToWall, Color.blue);
if (Physics.Raycast(transform.position, dir, out hit, distanceToWall))
{
if (hit.collider.gameObject.tag == "Sneaking Volumes")
{
if (animation.IsPlaying("P1TurnAgainstWall"))
{

TPController.characterController.Move(-hit.normal);

transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x,
Mathf.Lerp(transform.rotation.eulerAngles.y,
Camera.mainCamera.transform.eulerAngles.y,
.5f),
transform.rotation.eulerAngles.z);
}
}
}



Posted 08 May 2013 - 02:16 PM

Not sure I understand the question... do you mean rotating the character swings the camera round so it is looking through the wrong side of the wall? Do you want the player to be seen face on with his back against the wall?

What you should probably do in that case is not turn the character in code at all (except to align it to the wall as a lead in to the animation), put the rotation of the character into the animation instead, and keep the character facing to be towards the wall in the code. That would be more natural for controlling the character as well (i.e. you don't want the controls to flip left/right when you are leaning against a wall either).

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #3steviebops  Members

Posted 08 May 2013 - 02:53 PM

I have an animation that turns the player 180°back against the wall, the problem is locking the rotation when he's touching the wall.

The character rotates to snap to the camera direction for general movement.

With the animation, the player will face away from the wall, but will rotate with the camera.

transform.rotation = Quaternion.Euler(transform.eulerAngles.x,
Camera.mainCamera.transform.eulerAngles.y,
transform.eulerAngles.z);


After he locks to the wall

if (TPAnimator.Instance.State == TPAnimator.CharacterState.PressedWallStand)
{
if (animation.IsPlaying("P1WallHideStand"))
{
transform.rotation = Quaternion.Euler(transform.eulerAngles.x,
0,
transform.eulerAngles.z);
}
}


By replacing the camera's y transform, I think I can get the correct angle for each face, 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 can't figure out how to get the character to be parallel with each side, then stay that way.

Posted 08 May 2013 - 04:15 PM

You need to use the normal of the cube face you are stealthing upon, you can probably extract that from the collision data you get back from your raycast.

I wouldn't use Euler angles for this though, use vectors. You want to slerp your rotation to align with the normal of the cube face. If you must use Eulers, you need to convert the returned normal into an Euler angle representation.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #5steviebops  Members

Posted 09 May 2013 - 09:56 AM

Yup, that seems to have done it

I use this code

if (TPAnimator.Instance.State == TPAnimator.CharacterState.PressedWallStand)
{
if (animation.IsPlaying("P1WallHideStand"))
{
transform.rotation = Quaternion.FromToRotation(-transform.forward, TPAnimator.Instance.hit.normal) * transform.rotation;
}
}


And it seems fine so far. The collision stops the player from moving into the cube, and Ill add code later to stop them from pulling away without hitting the right button, or stealthing past edges.

thanks for the help!!

### #6steviebops  Members

Posted 15 May 2013 - 11:37 AM

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.

### #7SimonForsman  Members

Posted 15 May 2013 - 12:14 PM

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.

you could just do a raycast towards the wall (opposite direction of the normal) from the position the character is moving towards, then check that it hits the same object as a raycast from its current position.
I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

### #8steviebops  Members

Posted 15 May 2013 - 12:32 PM

The character already has their back to the wall, and are moving along it sideways. What Im trying to determine is, is it possible to get the length of that wall.
I have attached a picture to show the situation.

### #9SimonForsman  Members

Posted 15 May 2013 - 01:43 PM

The character already has their back to the wall, and are moving along it sideways. What Im trying to determine is, is it possible to get the length of that wall.
I have attached a picture to show the situation.WallProblem.png

you could get the dimensions of the collider your ray hits, or of the object it belongs to. if your wall colliders/objects have complex shapes it gets a bit tricky though.

The raycast method is probably the easiest way to do it with Unity though and it makes it very easy to deal with complex shaped walls. (the raycast from a the target position lets you know if you're about to move off the edge, how much the wall curves, etc).

You could also add a separate layer with colliders extending from the wall edges and only use that layer when the character is sneaking along walls. (For complex levels adding all those extra colliders could take quite alot of effort though but its easy to code (just use http://docs.unity3d.com/Documentation/ScriptReference/Physics.IgnoreLayerCollision.html)

Edited by SimonForsman, 15 May 2013 - 01:47 PM.

I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

### #10steviebops  Members

Posted 15 May 2013 - 03:13 PM

I might do the extra collision volume method. I've messed around with getting the dimensions from the collider, it's not working as of yet. Ill try again tomorrow.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.