The walking method is almost perfect now, i only have a problem with the hills(it just walkes through when its in the way) and the dephts(sorry, i couldn’t find a better word) where it just fly’s over. How can i make the script generating a path over the terrain instead of going through/flying over?
This is my code:
function Update () {
if(newPos != transform.position) {
Debug.DrawLine(newPos, transform.position, Color.yellow);
// walk to new position
distance = Vector3.Distance(newPos, transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(newPos - transform.position), rotateSpeed * Time.deltaTime);
transform.position += transform.forward * moveSpeed * Time.deltaTime;
// Don't let it walk around the new position
if(distance < 2.0f) {
newPos = transform.position;
}
} else {
// Don't walk away directly
if(Random.Range(0,100) == 1)
newWalkingPos();
}
}
function newWalkingPos () {
var newZ : float;
var newX : float;
var newY : float;
var hit : RaycastHit;
newZ = Random.Range(maxBottom, maxTop);
newX = Random.Range(maxLeft, maxRight);
if (Physics.Raycast (Vector3(newX, 100, newZ), -Vector3.up, hit)) {
newY = (100 - hit.distance) + 0.6;
}
newPos = Vector3(newX, newY, newZ);
}
Sorry for bad English, I got a ‘get the * up’-6 for English on highschool
You want to find the elevation of the terrain at a given x+y coordinate? If so, do a raycast down from the sky, and look at the hitinfo. If you’re dealing with actual terrain there might be a helper method in the terrain class for getting height at a point.
The walking method is almost perfect now, i only have a problem with the hills(it just walkes through when its in the way) and the dephts(sorry, i couldn’t find a better word) where it just fly’s over. How can i make the script generating a path over the terrain instead of going through/flying over?
This is my code:
function Update () {
if(newPos != transform.position) {
Debug.DrawLine(newPos, transform.position, Color.yellow);
// walk to new position
distance = Vector3.Distance(newPos, transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(newPos - transform.position), rotateSpeed * Time.deltaTime);
transform.position += transform.forward * moveSpeed * Time.deltaTime;
// Don't let it walk around the new position
if(distance < 2.0f) {
newPos = transform.position;
}
} else {
// Don't walk away directly
if(Random.Range(0,100) == 1)
newWalkingPos();
}
}
function newWalkingPos () {
var newZ : float;
var newX : float;
var newY : float;
var hit : RaycastHit;
newZ = Random.Range(maxBottom, maxTop);
newX = Random.Range(maxLeft, maxRight);
if (Physics.Raycast (Vector3(newX, 100, newZ), -Vector3.up, hit)) {
newY = (100 - hit.distance) + 0.6;
}
newPos = Vector3(newX, newY, newZ);
}