I have a terrain that I got from CityEngine and it has a mesh collider and when I created a first person controller it worked perfectly! But then I wanted to change it because I don’t want it to be controlled by the keyboard. The speed will only be controlled by signals from a virtual com-port. So I put another script on the first person controller. It works fine to make it move forward without pressing a button. But it falls through the terrain when it’s uphill. When its downhill or straight it follows the terrain good. Why doesn’t it work?
Here is my script I added:
public class PlayerController : MonoBehaviour {
public float forwardSpeed = 0.0f;
public float backwardSpeed = 5.0f;
public float rotateSpeed = 0.0f;
public int speed = 50;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Move forward
forwardSpeed = speed/5;
transform.position += transform.forward * forwardSpeed * Time.deltaTime;
//Turn
transform.Rotate(0.0f, rotateSpeed*Time.deltaTime, 0.0f);
//Turn left
if(Input.GetKey(KeyCode.LeftArrow))
{
rotateSpeed = -50.0f;
}
//Turn right
else if(Input.GetKey(KeyCode.RightArrow))
{
rotateSpeed = 50.0f;
}
//No turn
else
{
rotateSpeed = 0.0f;
}
//Quit application
if(Input.GetKey(KeyCode.Escape))
{
Application.Quit();
}
}
}
(The “speed” is the signal from the virtual com-port)