找到了怎么做,但为什么它移动如此之快?我怎么能让它移动得慢得多?试图将速度改为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,但立方体在非常小的区域上移动。
答案 0 :(得分:1)
Hen,添加一个用于控制速度的公共浮点数,然后在代码中使用Time.deltaTime。使用这两行
public float speed;
和
objects[0].transform.position = Vector3.Lerp(objects[0].transform.position,
GenerateRandomPositions(objects[0]), (Mathf.Sin(speed * Time.deltaTime)));
祝你好运。