hi guys. I am currently working an rts game.as of now my camera moves when the mouse pointer is scrolling but the problem is that the environment that i am using is a dessert in which the ground is uneven. Can you please tell me how the height of my camera would adjust to my terrain?
There may be more efficient ways to do this but I would do it this way
Set a max height for your camera
Cast a ray from your camera to the terrain (Probably -Vector3.up)
Get the position of the hit
Lerp your camera’s transform.position.y value to hit.position.y + maxheight
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
//Public Variables
public GameObject camera;
//Private Variables
private float maxHeight = 10;
// Update is called once per frame
void Update ()
{
RaycastHit hit;
if(Physics.Raycast(camera.transform.position, Vector3.down, out hit, 100))
{
if(hit.name == "Terrain")
{
camera.transform.position = new Vector3(camera.transform.position.x,
Mathf.Lerp(camera.transform.position.y,hit.point.y,Time.deltaTime),
camera.transform.position.z);
}
}
}
}