|
I am trying to make a script to make an object wander around. So far I have this:
This makes the object move to the position I want but my problem is that since it's a vector2 it operates between the x and y axis. I want my object to move 2 dimensionally (between the x and z axis). When I leave the var wayPoint undefined when the game first starts the entire script doesn't work. Why is this? additionally I tried to make a while statement and then replaced it with an if statement saying that while(or if) the object was not dead that it would "Wander". Any help is appreciated. Should the function Update() be a Fixed Update instead?
(comments are locked)
|
|
FixedUpdate should be reserved for physics related functions. You need to make wayPoint a global variable by declaring it outside of the function, put it at the top of your script, leaving it undefined obviously means that variable has no meaning and wont compile if you tried to use it in a function later e.g.
You can also use Radom.insideUnitSphere instead and then set the y variable to whatever height your floor is e.g.
Instead of using invoke you could simply call Wander after you've reached the waypoint and the character is still alive. e.g.
EDIT: Here's your working code... Thank you spinaljack, I am still having some difficulties so I responded in the form of an answer so I could post my new code.
Jul 22 '10 at 01:49 AM
Panamamigo
Thanks a ton man, it is working great!
Jul 22 '10 at 03:33 PM
Panamamigo
(comments are locked)
|
|
Here's an update to this, I was noticing it always defaulted to 0,0,0 and then picked waypoints from there. This will cause it to wander aimlessly from it's origin.
(comments are locked)
|
don't post an answer... edit your question
Jul 22 '10 at 01:54 AM
spinaljack
The reason it's moving slowly is because you're using force and the objects is either very heavy or has high friction. Change it to transform.position += transform.TransformDirection(Vector3.forward)*speed*Time.deltaTime
Jul 22 '10 at 01:56 AM
spinaljack
also you've put your actual movement code inside a function and not the update, it'll only move one every time the function is called instead of every frame like it should
Jul 22 '10 at 02:02 AM
spinaljack
I tried that, but it still isn't working with the transform.position += transform.TransformDirection...etc. When I use that it does not move at all. When I use other methods like transform.Translate or something it only moves a fraction of the way. What could be wrong? Thank you so much for all your help spinaljack it's really helping me out! I edited my question with the code.
Jul 22 '10 at 06:00 AM
Panamamigo
(comments are locked)
|
