0
votes
using UnityEngine;
using System.Collections;

public class WaypointsMover : MonoBehaviour {

    public float rotateSpeed = 2.0f;

    private bool rotating;

    void Update() {
        StartCoroutine(TurnTowards(-transform.forward));
    }

    IEnumerator TurnTowards(Vector3 lookAtTarget) {    

        if(rotating == false) {
            Quaternion newRotation = Quaternion.LookRotation(lookAtTarget - transform.position);
            newRotation.x = 0;
            newRotation.z = 0;

            for (float u = 0.0f; u <= 10.0f; u += Time.deltaTime * rotateSpeed) {
                rotating = true;
                transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, u);

                yield return null;
            }
            rotating = false;
        }
    }

    // Use this for initialization
    void Start () {

    }
}
  1. No matter if i change the value of rotateSpeed to 10.0f or to 20.0f the enemy will get to one side will walk some on place and then will turn and walk to the other side. How can i make that the enemy will turn right away when getting to the side ?

  2. Why the enemies positioned there on the building roof while in the scene i position them in another places at all ?

  3. How do i set waypoints ? I can't find in the code way points. I know that in the for loop it was in the original:

    for (float u = 0.0f; u <= 1.0f; u += Time.deltaTime * rotateSpeed) {

I changed it to 10.0f instead of 1.0f so now they are walking longer way on the roof. But if i want to make that they will go between two spheres i have ?

This is a screenshot of the scene the building on the right where they walk on it's roof when running the game. The two enemies(Guards) on the left on the right it's the Main Player. The camera i set it to follow the first Guard.

I also noticed there is not space between them so they collide each other or disturb each other to walk. Where or how can i set more space between them so each one will go on it's own path ?

Scene

This is a short video clip i recorded showing how they are walking and where they are walking:

Video Clip

1
1- Check your enemy (AI) code I am sure that you are setting the position somewhere. 2- I don't know what scripts does each enemy use, however I am quite sure that you have only one instance of the "WaypointsMover" (I think that is some kind of manager). If not, could you paste the enemy code and the components, please? Thanks. - Cabrra
@Cabrra I checked now i saw that the Terrain in the Navigation window in Object tab i checked to make it Navigation Static and then i clicked on Bake. So now the enemies start from the position they are in the Scene. But for some reason they are going first to be under the building and there they are doing the patrolling. I can't figure out why there are going to the building. - TheLost Lostit
@Cabrra I checked now i saw that the Terrain in the Navigation window in Object tab i checked to make it Navigation Static and then i clicked on Bake. So now the enemies start from the position they are in the Scene. But for some reason they are going first to be under the building and there they are doing the patrolling. I can't figure out why there are going to the building. - TheLost Lostit
@Cabrra The enemies are the Guards you can see in the screenshot in my question. I call them enemies but in my scenes i named them Guards(Guard and Guard 1). And they don't have any scripts or codes other then this one in my question. This is the only script attached to the Guard and Guard 1. This Guards are ThirdPersonControllers objects. - TheLost Lostit
@Cabrra How do i copy and paste over here the Guards components ? I can upload my project to my onedrive maybe. Or maybe to upload only the characters. - TheLost Lostit

1 Answers

0
votes

Have you tried to use rotateSpeed in the Quaternion.Slerp() function instead of var u? As Time.deltaTime has quite a low value (at first it should be smth around ~0.0145) it will lower the speed of rotation dramatically. And it won't feel different if the rotateSpeed is 10.0f or 20.0f as it will only be faster at the end (I haven't tried to execute it though)

Also, Slerp transitions the rotation to look smooth, so if you want an instant transition, just set the rotation of the object to the value you need.