1
\$\begingroup\$

I need to make some steps.

  1. To rotate both npcs but each time running the game they will rotate randomly from another direction. For example the first one will rotate from the right side or left side same the other one.

  2. They will facing the door and start moving to the door. Near the door she will open automatic they will wait then they will move through the door.

  3. one of the npcs will keep moving and will start random patrolling.

  4. The second npc will rotate facing the door from the outside and will do something like locking the door ( I will animate it later he will rotate facing the door wait 2-3 seconds will rotate and will join the first npc and start patrolling too.

This is a screenshot of both npcs each one have a box collider. The door they should rotate smooth randomly and facing and moving to the door the one behind them.

Npcs

This is the behind the door outside the hall. This is where one of the npcs should start patrolling and the other one wait some seconds facing the door like he is locking the door and then after some seconds to join and start patrolling too.

Npcs

And this is the hall out side the door and then the hall right and left. They should be patrolling the hall right and left and also the hall with the door. But not getting too close to the door/s so they will not open all the time.

The last screenshot show where they should be patrolling. The left image is the hall outside the door the right image is a door on the front hall on the right and the bottom image the door on the left.

It's like a T

Npcs

I need some guide how to start it by logic how to do it. I have the scenario in my mind what they should do the 4 steps. But not surw hot start doing it.

The scenario in general is: Two guards(npcs) lock the door from the outside and start patrolling outside.

\$\endgroup\$
1
\$\begingroup\$

For the rotation you should look at the MoveRotation documentation for unity. This will allow you to smoothly rotate a rigidbody a certain way. You can use a random number to determine which way the will rotate like:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    public Rigidbody rb;
    public float randRotation; 
    float speed = 1.0f;

    void Start()
    {
        randRotation = Random.Range( 0, 1); //0 or 1
        rb = GetComponent<Rigidbody>();
    }

    // fixedDeltaTime provides the same time that FilterUpdate() uses
    void FixedUpdate()
    {
        if (randRotation == 0)
        {
             //Rotate character left
        }else{
             //Rotate character right
        }
    }
}

What I recommend for you to take a look at for the patrolling is the usage of a waypoint system. If you want all guard to patrol that is. It basically is an array of points which the guard shall walk too. You can make this random or structured.

I found a good answer on the Unity forum Just to be sure I will copy the code he showed into this answer in case of removal:

public class ClassName: MonoBehaviour {


 public Transform[] waypoint;        // The amount of Waypoint you want
 public float patrolSpeed = 3f;       // The walking speed between Waypoints
 public bool  loop = true;       // Do you want to keep repeating the Waypoints
 public float dampingLook= 6.0f;          // How slowly to turn
 public float pauseDuration = 0;   // How long to pause at a Waypoint

 private float curTime;
 private int currentWaypoint = 0;
 private CharacterController character;

 void  Start (){

     character = GetComponent<CharacterController>();
 }

 void  Update (){

     if(currentWaypoint < waypoint.length){
         patrol();
     }else{    
         if(loop){
             currentWaypoint=0;
         } 
     }
 }

 void  patrol (){

     Vector3 target = waypoint[currentWaypoint].position;
     target.y = transform.position.y; // Keep waypoint at character's height
     Vector3 moveDirection = target - transform.position;

     if(moveDirection.magnitude < 0.5f){
         if (curTime == 0)
             curTime = Time.time; // Pause over the Waypoint
         if ((Time.time - curTime) >= pauseDuration){
             currentWaypoint++;
             curTime = 0;
         }
     }else{        
         var rotation= Quaternion.LookRotation(target - transform.position);
         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
         character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
     }  
 }
 }

As for the locking the door I recommend making a state machine for your guards so that you can assign a close door state or something along those lines. Which you then fill with the data you want regarding it closing a door.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.