-1
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityStandardAssets.Characters.ThirdPerson;

public class Multiple_objects : MonoBehaviour {

    public GameObject prefab;
    public GameObject[] gos;
    public int NumberOfObjects;
    private ThirdPersonCharacter[] thirdPersonCharacter;
    private Animator[] _animator;
    private int count = 0;
    private List<float> floats = new List<float>();

    public float smooth = 1f;
    private Vector3 targetAngles;

    void Awake()
    {       
        Vector3 v3 = prefab.transform.position;
        _animator = new Animator[NumberOfObjects];
            gos = new GameObject[NumberOfObjects];
        for(int i = 0; i < gos.Length; i++)
        {
            count = count + 2;
            GameObject clone = (GameObject)Instantiate(prefab, Vector3.zero, Quaternion.identity);
            gos [i] = clone;
            gos [i].transform.position = new Vector3 (v3.x - count, v3.y, v3.z);
            _animator [i] = gos[i].GetComponent<Animator> ();
            float randomspeed = 0f;
            // Keep repeating this until we find an unique randomspeed.
            while(randomspeed == 0f || floats.Contains(randomspeed))
            {
                randomspeed = UnityEngine.Random.Range(1.0f, 15.0f);
            }
            floats.Add (randomspeed);
            //float vertInput = 1.0f;
            _animator [i].SetFloat ("Speed", randomspeed);
            if (randomspeed != 0.0f)
                _animator[i].speed = randomspeed;
            else
                _animator [i].speed = 1.0f;
        }
    }

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

        if(Input.GetKeyDown(KeyCode.S)) // some condition to rotate 180
            targetAngles = gos[5].transform.eulerAngles + 180f * Vector3.up; // what the new angles should be

        transform.eulerAngles = Vector3.Lerp(gos[5].transform.eulerAngles, targetAngles, smooth * Time.deltaTime); // lerp to new angles


    }
}

In the Hierarchy i have two ThirdPersoncontroller: ThirdPersonController and ThirdPersonController(1)

I put the script on the ThirdPersonController and the prefab is the ThirdPersonController(1) then it's cloning more 15 ThirdPersonControllers.

Then each of the cloned 15 players walk in another speed. Now in the Update function when i press on the S key the ThirdPersonController is turning 180 degrees and then all the cloned 15 players also turn 180 degrees and keep walking to this direction.

What i want to do is when one of the cloned players getting to Z position turn 180 degrees only this player and turn on place 180 degrees for a second ot two walk on place turn then continue walking to the turned direction.

So in the Update function i'm using now the S key as condition but it's turning the ThirdPersonController and all the clones turn according to him and walk to the turned direction.

I tried to use gos[0].transform also gos[5].transform but it's not turning only the fifth clone but turn all of them 180 degrees.

And the condition i tried to make if(gos[0].transform.position.z == 108.9377f) or if(gos[0].transform.position.z == 100) but it's never get to the next line i used a break point.

TheLost Lostit
  • 505
  • 6
  • 28

1 Answers1

0

Rather than checking when you reach that exact value, I'd check for distance between you and the waypoint using Vector3.Distance

Now for the reason all the characters are walking the same action is because of this line :

 transform.eulerAngles = Vector3.Lerp(gos[5].transform.eulerAngles, targetAngles, smooth * Time.deltaTime);

You'd want to do something like this in your update

for(int i = 0; i < gos.Length; i++)
{
    //if(Input.GetKeyDown(KeyCode.S)) // some condition to rotate 180
    //    targetAngles = gos[i].transform.eulerAngles + 180f * Vector3.up;

    // If the Distance between gos[i] and Vector3(0,0,100) is smaller than 3f
    if(Vector3.Distance(gos[i].transform.position, new Vector3(0,0,100)) < 3f)
    {
        targetAngles = gos[i].transform.eulerAngles + 180f * Vector3.up;
    }

    gos[i].transform.eulerAngles = Vector3.Lerp(gos[i].transform.eulerAngles, targetAngles, smooth * Time.deltaTime);
}
spatbord
  • 71
  • 6
  • I just stumbled upon your next question : http://stackoverflow.com/questions/38681737/how-can-i-make-my-waypoints-to-work where you seems to be using my answer. Could you please mark this question as answered? – spatbord Jul 31 '16 at 20:38