Trying to get a character to jump around on a terrain with different heights.

Hi, in my Game Art 2 class at school we are recreating Slender and I need some help. We have gotten Slender to “jump” around the map until he is close enough to you. But the problem is that the terrain is at different heights and he is spawning at one single height. Here’s what I have

public class Teleporter : MonoBehaviour {

public Transform _player; //the object the player is controlling
public Vector3 _spawnOrigin; //where slender spawns
public Vector3 _maximum; //max distance x,y,z slender can move
public float _spawnRate; //slender jumping.
public float _distanceToPlayer; //how close slender is to the player
public bool _nearPlayer; //starts and stops teleporting
private float _nextTeleport; //keeps track of when to teleport

// Use this for initialization
void Start ()
{
_nextTeleport = _spawnRate;
}

// Update is called once per frame
void Update ()
{

if (!_nearPlayer)
{
if (Time.time > _nextTeleport)
{
//long i know, this should make slender jump
transform.position = new Vector3(Random.Range(_spawnOrigin.x,_maximum.x),
Random.Range(_spawnOrigin.y,_maximum.y),
Random.Range(_spawnOrigin.z,_maximum.z));
_nextTeleport += _spawnRate;

}
if (Vector3.Distance (transform.position, _player.position) <= _distanceToPlayer)
{
_nearPlayer = true;
}
else
{
_nearPlayer = false;
}
}
}
}

Thank you to anyone who can help solve my probelm!!

You could select a random X,Z coordinate and then, from an arbitrarily high distance, do a raycast against the terrain to determine its elevation. I’ve never tried it, honestly nor have I done much raycasting, but that should work.

Any idea how to put that into my code? I’ve used raycasts before I’m just not sure how to put it in there and make it work

code tags when posting code please…

Here is one implementation of what you are thinking.

public class Teleporter : MonoBehaviour {
	public Transform _player; //the object the player is controlling
	public Vector3 _spawnOrigin; //where slender spawns
	public float _maximum = 10; //max distance x,y,z slender can move
	public float _spawnRate; //slender jumping.
	public float _distanceToPlayer; //how close slender is to the player
	public bool _nearPlayer; //starts and stops teleporting
	private float _nextTeleport; //keeps track of when to teleport
	
	// Use this for initialization
	void Start (){
		_nextTeleport = _spawnRate;
	}
	
	// Update is called once per frame
	void Update (){
		if (!_nearPlayer){
			if (Time.time > _nextTeleport){
				transform.position = GetNextPoint(transform.position);
				_nextTeleport += _spawnRate;
			}
			_nearPlayer = (transform.position - _player.position).magnitude <= _distanceToPlayer;
		}
	}
	
	Vector3 GetNextPoint(Vector3 origin){
		// try up to 10 times to find a point.
		for(var i=0; i<10; i++){
			Vector2 move = Random.insideUnitCircle * _maximum;
			Vector3 newPos = new Vector3(move.x, 10000, move.y);
			Ray ray = new Ray(origin + newPos, Vector3.down);
			RaycastHit hit;
			
			// test the physics
			// alternative, test the ground collider only.
			if(Physics.Raycast(ray, out hit)){
				// you may want to try a Physics.OverlapSphere to see if any objects are actually there.
				// this would prevent him from appearing in a tree, or building.
				return hit.point;
			}
		}
		return origin; // couldn't find a move within the time frame.
	}
}

The checks could include, not being inside of another object and also you could include not being in the camera’s visual arc.

That worked perfectly. Thank you!