I am writing my controller in C #, the problem is that the character goes even over very high mountains. Here is the code:

void Update () { if (Input.GetKey("w")) { transform.position += transform.forward*10*Time.deltaTime; } if (Input.GetKey("s")) { transform.position += transform.forward*-10*Time.deltaTime; } if (Input.GetKey("escape")) { Application.Quit(); } } 
  • one
    Awful. You have mixed business logic ( Application.Quit() ) and UI-part (keyboard processing) in one pile. Read about MVC / MVP. - VladD

2 answers 2

It seemed to me alone that controllers are always written in physics, and not just in transformations? Rigidbody.AddForce() and you will be happy (or just Rigidbody look in the Unity Script Reference )

  • one
    Yes, I think to you alone :) For a character, if this is just a person, and not some kind of technique, physics is better and not used, otherwise it will be very difficult for you to control it. You have to control the object or only physics or only transform, otherwise it will be scary when you add strength to it and move it somewhere else. In unity, just for characters, there is a Character Controller, it processes physics, it does not allow it to fail or pass through the wall and at the same time the character moves just by moving, without rigidbody and adding force. - vinnie

The question is a little not correct. If you go back to our world, what does the mountain mean too high, that a person stepping on it should slide down? No matter how much a person walks on a mountain, he can walk along it again and again, the height of the mountain will not stop him at all. Rather, it may limit the angle of the mountain if it does not have the appropriate equipment. If you want to restrict your character to travel in the mountains with a certain inclination, then this is already implemented in Unity for you. You must add a Character Controller to your character, and move the character to it, it has a Move method. And he will take care of the whole physics, he has the Slope Limit parameter. I suppose that is what you need.

Unity Manual Character Controller

Character Controller Move