- Home /
Question by
Chocolade · Oct 17, 2017 at 03:48 PM ·
c#scripting problemscript.
How can i move object between given random positions ?
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 Vector3 startPos;
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);
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));
newObject.transform.localPosition = new Vector3(posx, posy, posz);
objects.Add(newObject);
currentObjects += 1;
}
var rnd = UnityEngine.Random.Range(5, 100);
var rnd1 = UnityEngine.Random.Range(5, 100);
objects[0].transform.position = Vector3.Lerp(objects[0].transform.position, new Vector3(rnd, objects[0].transform.position.y,rnd1), (Mathf.Sin(20f * Time.time)));
}
}
The part is added now for testing is:
var rnd = UnityEngine.Random.Range(5, 100);
var rnd1 = UnityEngine.Random.Range(5, 100);
objects[0].transform.position = Vector3.Lerp(objects[0].transform.position, new Vector3(rnd, objects[0].transform.position.y,rnd1), (Mathf.Sin(20f * Time.time)));
But it's moving very very fast and at the 5,100 random positions of the terrain. But i want it to move in random positions only inside the walls area. This is the walls variables:
private int wallsLengthX;
private int wallsLengthZ;
private int wallsPosX;
private int wallsPosZ;
I want that every newObject created to move in random positions only inside the walls area. Now i'm trying to work with one object from the List just for testing. But it's not working yet.
Comment
I found how to do it but why it's moving so fast ? How can i make it move much slower ? Tried to change the speed from 20 to 0.3f but still very fast. I want to be able to control the speed from 0 not moving at all to very fast like it is now.
$$anonymous$$aybe not using Vector3.Lerp and using Translate ins$$anonymous$$d ?
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnObjects : $$anonymous$$onoBehaviour
{
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]), ($$anonymous$$athf.Sin(20f * Time.time)));
}
private Vector3 GenerateRandomPositions(GameObject newObject)
{
float paddingX = $$anonymous$$athf.Clamp(newObject.transform.localScale.x, 0, wallsLengthX) / 2f;
float paddingZ = $$anonymous$$athf.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);
}
}