2017-10-17 60 views
0

找到了如何做到这一点,但它为什么这么快?我怎样才能让它移动得慢很多?试图改变速度为0.3f,而不是20,但仍然太快。如何将对象移动到某个随机位置,然后在到达新位置时旋转并移动到新的随机位置?

也许不使用Vector3.Lerp,但翻译? 我希望能够控制从非常缓慢的速度,即使是0也不会像现在这样非常快地移动。

using System; 
using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class SpawnObjects : MonoBehaviour 
{ 
    public int numberOfObjects; 
    public GameObject objectToPlace; 
    public Vector3 newObjectsSize = new Vector3(5,5,5); 

    private int currentObjects; 
    private int wallsLengthX; 
    private int wallsLengthZ; 
    private int wallsPosX; 
    private int wallsPosZ; 
    private List<GameObject> objects = new List<GameObject>(); 

    void Start() 
    { 
     var wi = GetComponent<WallsTest>(); 
     wallsLengthX = (int)wi.lengthX; 
     wallsLengthZ = (int)wi.lengthZ; 
     wallsPosX = (int)wi.wallsStartPosition.x; 
     wallsPosZ = (int)wi.wallsStartPosition.z; 
    } 
    // Update is called once per frame 
    void Update() 
    { 
     if (currentObjects != numberOfObjects) 
     { 
      GameObject newObject = (GameObject)Instantiate(objectToPlace);//(GameObject)Instantiate(objectToPlace, new Vector3(posx, posy, posz), Quaternion.identity); 
      newObject.transform.localScale = new Vector3(newObjectsSize.x, newObjectsSize.y, newObjectsSize.z); 
      newObject.transform.localPosition = GenerateRandomPositions(newObject); 

      objects.Add(newObject); 

      currentObjects += 1; 
     } 


     objects[0].transform.position = Vector3.Lerp(objects[0].transform.position, GenerateRandomPositions(objects[0]), (Mathf.Sin(20f * Time.time))); 
    } 

    private Vector3 GenerateRandomPositions(GameObject newObject) 
    { 
     float paddingX = Mathf.Clamp(newObject.transform.localScale.x, 0, wallsLengthX)/2f; 
     float paddingZ = Mathf.Clamp(newObject.transform.localScale.z, 0, wallsLengthZ)/2f; 
     float originX = wallsPosX + paddingX - wallsLengthX/2f; 
     float originZ = wallsPosZ + paddingZ - wallsLengthZ/2f; 
     float posx = UnityEngine.Random.Range(originX, originX + wallsLengthX - paddingX); 
     float posz = UnityEngine.Random.Range(originZ, originZ + wallsLengthZ - paddingZ); 
     float posy = Terrain.activeTerrain.SampleHeight(new Vector3(posx, 0, posz)); 

     return new Vector3(posx, posy, posz); 
    } 
} 

这是我记录一个很短的视频剪辑现在同时使用Vector3.MoveTowards或Vector3.Lerp时显示立方体运动行为。

在这两种情况下,当我将Lerp中的速度更改为非常快时,它将在墙壁区域快速移动,当我将速度更改为非常慢时,立方体将移动到中心并在极限区域快速移动。但立方体应在墙壁之间的区域周围移动缓慢。相反,它的速度很快,但面积很小。不知道为什么。

使用MoveTowards时相同。

我想让多维数据集移动的行为是,当我将速度更改为非常缓慢或快速,或者非常快速地移动到墙壁区域周围的随机位置时。在墙壁区域内。 在视频中,墙壁是500x500,但立方体在很小的区域内移动。

Move

回答

0

母鸡,增加公众持股量来控制速度,然后在你的代码中使用Time.deltaTime。使用这两条线

public float speed; 

objects[0].transform.position = Vector3.Lerp(objects[0].transform.position, 
GenerateRandomPositions(objects[0]), (Mathf.Sin(speed * Time.deltaTime))); 

好运。

+0

这是我录制的非常短的视频剪辑,现在显示使用Vector3.MoveTowards或Vector3.Lerp时的立方体移动行为。 在这两种情况下,当我将Lerp中的速度更改为非常快时,它会在墙壁区域快速移动,当我将速度更改为非常慢时,立方体将移动到中心并在非常有限的区域内快速移动。但立方体应在墙壁之间的区域周围移动缓慢。相反,它的速度很快,但面积很小。不知道为什么。 使用MoveTowards时相同。 https://www.youtube.com/watch?v=OSO63iw3wH4&feature=youtu.be –