- Home /
How can i move the objects once they are created in random positions but only inside a specific given area ?
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;
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);
currentObjects += 1;
}
}
private void MoveObjectsAround(GameObject go1, Vector3 position)
{
var facingdirection = go1.transform.forward;
var distance = Vector3.Distance(go1.transform.position, go2.transform.position);
go1.transform.position += go1.transform.forward * Time.deltaTime * UnityEngine.Random.Range(1,10);
var qTo = Quaternion.Euler(new Vector3(0.0f, UnityEngine.Random.Range(-180.0f, 180.0f), 0.0f));
go1.transform.rotation = Quaternion.Slerp(transform.rotation, qTo, Time.deltaTime * UnityEngine.Random.Range(1, 10));
}
}
What i want is once a newObject has positioned he will start moving to random positions around the area the walls area. Not on all the terrain only inside the walls area. And then the object/s will keep moving around randomly non stop.
Once the object got to the new random position rotate the object smooth into a random Quaternion and then move to the direction the object is facing now and so on. Move random around and rotate random.
And each time the object has rotated give him a new random position to move to.
I tried to add the method MoveObjectsAround but i messed it up.
Answer by haruna9x · Oct 17, 2017 at 02:33 AM
Spawner should only have one mission: spawn gameobject
GameObject need to manually move itself.
Spawer: To avoid overlapping spawn, simply use Raycast. You can find out more here
Move: you are talking to a waypoint system. You can find out more here.
Here I just talk about how it works
Step 1: Get a target. (taken from the given position, or random).
Step 2: Rotate gameobjec to face the target.
Step 3: Move to the goal.
Step 4: Check the distance, if it is less than a deltaPos, then the move is complete.
Step 5: Return Step 1.
To avoid overlapping locations when moving, simply use Raycast. You can find out more here.
Your answer
![](https://koobas.hobune.stream/wayback/20220612141216im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can i create array of texture2d with variables names ? 2 Answers
How can i rotate object by pressing on key R and keep object facing to me my self ? 0 Answers
How can i spawn new gameobjects to be inside the terrain area ? 2 Answers
How can i change vertex posiiton to push it back -1 on z ? 1 Answer