- Home /
Constantly randomize and update object's position
Hi,
I am trying to write a simple wandering AI script. Basically I have decided to make a box the target of my character, and when he reaches the box, the box will pick a new random position on the map within a set area. Here is my script:
using UnityEngine; using System.Collections;
public class SelfSpawnning : MonoBehaviour {
private int posX; // positions for waypoint object
private int posY;
private int posZ;
public Vector3 pos;
public Transform waypoint; // box that the character will seek
public bool reachedWaypoint; // whether charater reached the waypoint
// if reached it, randomize new position for waypoint
void Awake(){
reachedWaypoint = false;
SeedPos(); // seeds initial position for waypoint
waypoint.position = pos;
Instantiate(waypoint);
}
// Use this for initialization
void Start () {
reachedWaypoint = false;
}
// Update is called once per frame
void Update () {
if (reachedWaypoint) // do we need to find a new pos for the waypoint?
SeedPos(); // if so we seed new position
}
void SeedPos(){
posX = Random.Range(-25, 25);
posY = 5;
posZ = Random.Range(-25, 25);
pos = new Vector3(posX, posY, posZ);
waypoint.transform.position = new Vector3(posX, posY, posZ);
reachedWaypoint=false;
}
}
It runs error free (all you need to do is drag some transform into the public var area for "waypoint". However, I want the position to change each time you make the reachedWaypoint variable true. The Vector3 pos generates new coordinates by calling SeedPos(); but the position of the waypoint doesnt change. If I try to go waypoint.position.x = posX, I get an error, saying I can't modify that value for some reason.
How can I go about making my object move and what is the reason for that error? Thanks for any input, much appreciated.
Answer by gabs · Feb 09, 2012 at 01:53 PM
To modify the position, you can't do:
transform.position.x = value;
You need to store the position in a Vec3 first:
Vector3 p = transform.position;
p.x = value;
transform.position = p;
That's because the "transform.position" is a getter (not a publicly exposed variable), so you can't set it's child values like that.
Answer by B1uEM4oM4o · Feb 10, 2012 at 07:59 AM
Ok, thanks, that makes sense now, I'll try that. Appreciated!
Answer by B1uEM4oM4o · Feb 10, 2012 at 07:59 AM
Ok, I'm still having a bit of trouble. I did what you suggested,
void SeedPos(){
pos = waypoint.position;
pos.x = Random.Range(-25, 25);
waypoint.position = pos;
}
I can call this function by checking a bool in the inspector. I can see the Vector3 positions changing in the inspector too since they are public but the position of the transform doesn't update for some reason. Why would this be? Note the script is attached to the main camera, and it instantiates a prefab, waypoint, and moves it with the above function. Why doesn't it update and move instead of just changing the values?
Your answer
Follow this Question
Related Questions
Move character's position to middle of object 1 Answer
Unity Serializer Scene Only? 1 Answer
getcomponent' is not a member of 'object' 2 Answers
How to call OnGUI() on class object 1 Answer
Object spins out of control 1 Answer