determine what object is in front of character

Hi,

Am working in a group and am trying to create a script to:

  1. Detect if an object is in front of the character
  2. If object is in front of character, how high is the object
  3. Play animation based on whether there is an object in front of the player or not

This would be similar to battlefield 3, where the player is running, and when jumping, if there is nothing in front of the player, it does a normal jump. If there is a low obstacle such as a wall or fence, it does a one-handed vault over the fence.

What would be a good way to detect the object and determine which animation to play based on the object height that is in front of the character?

raycasting might be the thing you are looking for.

what happens if the objects can be different size? If I raycast, there might be a wall with a hole at the specific location that the raycast happen to be.

There is only raycasting for solving such problem.

Jacksmith, I would just add a script to the climbable objects with a Height property that you can fetch when you raycast it.

if (raycast.hit.target.height >=1)
{
Do crazy ninja jump;
}

Actually I would create a class for all objects with some generic properties like height and isClimbabble as a bool orr something like that. You know keep it extendable and flexible.

Something like that :slight_smile:

If you don’t want to use a raycast, you might be able to make a trigger object work.

  1. Parent a large cube in front of the character. Make sure the cube has a collider, and it’s set to be a trigger.
  2. On that cube, do a check for collisions every FixedUpdate. (Using Artr’s technique by giving the climbable objects a height value sounds like a great plan.)
  3. If the cube does detect a collision, and it knows the height of the blocking object, you can change the jump accordingly.

I haven’t tested this, but it may be a good alternative to raycasting.

ok. thanks.