如何在行走时让角色在这个地方转180度?

时间:2016-07-31 03:31:31

标签: c# unity3d unityscript unity5

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


    }
}

在Hierarchy中我有两个ThirdPersoncontroller:ThirdPersonController和ThirdPersonController(1)

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

然后每个克隆的15名球员以另一种速度行走。 现在在Update功能中,当我按下S键时,ThirdPersonController转动180度,然后所有克隆的15个玩家也转180度并继续向这个方向行走。

我想要做的是当其中一个克隆玩家到达Z位置时,只有这个玩家转180度,然后在第二个地方转动180度,然后继续向转向方向行走。

所以在更新功能中,我现在使用S键作为条件,但是它正在转动ThirdPersonController并且所有克隆都根据他转向并走向转向。

我尝试使用gos [0] .transform也gos [5] .transform但它不仅仅转动第五个克隆,而是将它们全部旋转180度。

我尝试制作的条件if(gos [0] .transform.position.z == 108.9377f)或if(gos [0] .transform.position.z == 100)但是它永远不会到达下一行,我使用了一个断点。

1 个答案:

答案 0 :(得分:0)

我不会检查您何时达到完全值,而是使用Vector3.Distance

检查您与航点之间的距离

现在因为这一行,所有角色都走了同样的动作:

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

您希望在更新中执行类似的操作

import { Component } from '@angular/core';
import { Item }    from './../item';
import { ItemComponent }    from './ItemComponent';

@Component({
  selector: 'Item-Set',
  directives: [ItemComponent],
  template: '<div class="singleItem" *ngFor="let eachItem of itemList"><Item></Item></div>'
})

export class ItemSet{
  itemList: Item[];
  constructor(){
    this.itemList=ItemList;
  }
}