我如何让一个角色在走路时在这个地方转180度

本文关键字:走路 180度 方转 角色 一个 | 更新日期: 2023-09-27 17:59:17

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

    }
}

在层次结构中,我有两个ThirdPersoncontroller:ThirdPersoncontroller和ThirdPersoncontroller(1(

我把脚本放在ThirdPersonController上,预制件是ThirdPersonControl(1(,然后它克隆了更多的15个ThirdPersonControllers。

然后,被克隆的15名玩家中的每一位都以另一种速度行走。现在,在更新功能中,当我按下S键时,ThirdPersonController正在旋转180度,然后所有克隆的15名玩家也旋转180度并继续朝这个方向走。

我想做的是,当其中一名克隆球员到达Z位置时,只将该球员旋转180度,并将场地旋转180度一秒或两次,然后继续向转弯方向行走。

因此,在更新功能中,我现在使用S键作为条件,但它正在转动ThirdPersonController,所有克隆都会根据他转动,并朝着转动的方向走去。

我试着用gos[0]。transform也用gos[5]。transform,但它不仅转动了第五个克隆,而且把所有克隆都转动了180度。

如果(gos[0].transform.position.z==108.9377f(或如果(gos[0].transform.position.z==100(,但它永远不会到达下一行,我使用了一个断点。

我如何让一个角色在走路时在这个地方转180度

与其检查何时达到精确的值,不如使用Vector3.distance 检查您与航路点之间的距离

现在,所有角色都在做同样的动作,是因为这句话:

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

你想在你的更新中做这样的事情

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);
}