Page 1 of 1

RayCasting and Player detection

Posted: Sat May 18, 2013 11:09 pm
by mohammadAdnan
Hello teacher Norbo , I have used rayCast methode as shown in the code below to detect player "Me" so that enemy take behaviour (attack , jump ,run ,crouch ......etc)

Code: Select all

if (enemyRay.Intersects(cci.CharacterController.Body.CollisionInformation.BoundingBox) <= 200)
            {
                RunController(dwarfAnimator, walk);
                    enemyChrachterController.Body.ApplyImpulse(enemyChrachterController.Body.OrientationMatrix.Forward,
                        Vector3.Normalize(enemyRay.Direction) * 100.0f);
                   //enemyChrachterController.HorizontalMotionConstraint.MovementDirection =
                     // Vector2.Normalize(new Vector2(dwarfPosition.X, dwarfPosition.Z)) * enemyChrachterController.Body.Mass;
            }
          
            enemyRay.Position = enemyChrachterController.Body.Position;
            enemyRay.Direction = Vector3.Normalize(enemyChrachterController.Body.OrientationMatrix.Right);
            if (enemyRay.Intersects(cci.CharacterController.Body.CollisionInformation.BoundingBox) <= 200)
            {
                RunController(dwarfAnimator, walk);
                    enemyChrachterController.Body.ApplyImpulse(enemyChrachterController.Body.WorldTransform.Forward,
                        Vector3.Normalize(enemyRay.Direction) * 150.0f);
            }
            
            enemyRay.Position = enemyChrachterController.Body.Position;
            enemyRay.Direction = Vector3.Normalize(enemyChrachterController.Body.WorldTransform.Forward);
            if (enemyRay.Intersects(cci.CharacterController.Body.CollisionInformation.BoundingBox) <= 200)
            {
                RunController(dwarfAnimator, walk);
                    enemyChrachterController.Body.ApplyImpulse(enemyChrachterController.Body.WorldTransform.Forward,
                        Vector3.Normalize(enemyRay.Direction) * 150.0f);
            }
            
            enemyRay.Position = enemyChrachterController.Body.Position;
            enemyRay.Direction = Vector3.Normalize(enemyChrachterController.Body.WorldTransform.Backward); 
            if (enemyRay.Intersects(cci.CharacterController.Body.CollisionInformation.BoundingBox) <= 200)
            {
                RunController(dwarfAnimator, walk);
                    enemyChrachterController.Body.ApplyImpulse(enemyChrachterController.Body.WorldTransform.Forward,
                        Vector3.Normalize(enemyRay.Direction) * 150.0f);
            }
Now it works very very very bad as I must be in front , right , left , forward enemy so that he will attack !! so that any one play game must wate and be exactly in one of the previous positions so that ray detect him and enemy start to chase him !!!!

Norbo how to improve that and make it detect player while it's in the range of 100 meter and so on

Image

Re: RayCasting and Player detection

Posted: Sat May 18, 2013 11:29 pm
by Norbo
You can get much more in-depth information with some AI-dedicated resources elsewhere, but here's one option that works well: instead of trying to simulate the full senses of an NPC in a 'forward' manner, go backwards. In other words, the computer knows exactly where the player is, you just have to decide if the NPC should also have that information. For example, cast a ray from the NPC directly to the player to check if the player can be seen by the NPC.