- Home /
Question by
Kayfish · Mar 19, 2014 at 08:16 PM ·
movement script
Problem With Wandering Script and Terrain.SampleHieght
I have been trying to get a basic wandering script done for a project I have been working on. I got everything to work except my terrain.SampleHieght, when even I add it in it causes my object to go off indefinitely in one direction along the Z axis. Any advice is helpful and appreciated.
#pragma strict
var Speed = 3;
private var wayPoint : Vector3= Vector3.zero;
var circleSize = 20;
//variables speed, circlesize, and range of Vectors for waypoint
function Start()
{
random();
}
function Update ()
{
transform.position += transform.TransformDirection(Vector3.forward)*Speed*Time.deltaTime;
if ((transform.position - wayPoint).magnitude < 1)
{
//movement changes direction on approching a certain distance ot the waypoint
random();
}
}
function random (){
var chooseWayPoint : Vector3 = Random.insideUnitSphere * (circleSize);
wayPoint.y = Terrain.activeTerrain.SampleHeight(wayPoint)+ Terrain.activeTerrain.GetPosition().y;
wayPoint = new Vector3 (chooseWayPoint.x, wayPoint.y, chooseWayPoint.z);
//set the sphere of allowed movement
transform.LookAt(wayPoint);
}
Comment
Answer by soulreafer22 · Dec 02, 2015 at 03:17 AM
any success?
Do you need some help with a similar problem or are you just curious? lol its been a while but I think I did end up solving it. If you do need help I can dig up the code.
Never$$anonymous$$d. I got it working for myself :-P Greetings