- Home /
Third person camera, movement on terrain
Hey guys, i'm quiet new to unity, but not to programming. I am trying to make a simple third person game and already have problems with the camera and the movement on terrain. So the camera should always be behind the player and you change the direction of the player by turning arround the mouse. Then you can move with wasd. The problem with the camera is, that it kinda shakes. The problem about movement is, that i need to walk on terrain or just angular planes but also need walls, which i can't pass. First thing i tried was manipuating the position of the player directly using translate. Now i could walk on terrain perfectly, but could just walk on top of my walls. Then i tried to manipulate the velocity of my rigidbody so i couldn't pass walls but also couldn't walk up on the terrain. I already looked at the unity projects about third person, but they are way to complicated and i also don't need any animations or stuff like that. Does anyone of you have a piece of code or any tipps on how to do it as simple as possible?
Thanks, stevey
Are you using fixedupdate ? 90% of shaking is from using Update.
thank you for that tipp, but it didn't solve my problem. Heres the code i use:
Camera.main.transform.localEulerAngles = new Vector3(Camera.main.transform.rotation.eulerAngles.x, playerParent.transform.rotation.eulerAngles.y, 0);
Camera.main.transform.position = playerParent.transform.position; Camera.main.transform.Translate(Vector3.back * 6);
playerRotationY = playerParent.transform.rotation.eulerAngles.y; cameraRotationX = Camera.main.transform.rotation.eulerAngles.x;
playerRotationY += Input.GetAxisRaw("$$anonymous$$ouse X") * GameConstants.movementCameraSensitivity * Time.deltaTime;
cameraRotationX -= Input.GetAxisRaw("$$anonymous$$ouse Y") * GameConstants.movementCameraSensitivity * Time.deltaTime;
playerParent.transform.localEulerAngles = new Vector3(0, playerRotationY % 360, 0);
Camera.main.transform.localEulerAngles = new Vector3(cameraRotationX % 360, playerParent.transform.rotation.eulerAngles.y, 0);
Answer by sparkzbarca · Mar 04, 2013 at 07:05 PM
use charactercontroller and the move commands to move
there are 3 types of movements you can do in unity
teleports- aka anything ending in .position = ....
physics- using the rigidbody components (add force etc)
charactercontroller- non phyics movement BUT INCLUDES collision detection. if you can wrap the collider around your person use this for your game.
then do player.charactercontroller.move(where to go)
ohh, wow i didn't know that something like this existed, but thats exactly what i needed. But now i still got the problem about the shaking camera...
thanks, stevey
EDIT: now i could solve the camera problem on my own. I just had to change the sequenze of the code a little bit and put everything into the lateUpdate method and everything works fine. Heres the code:
void LateUpdate() {
playerRotationY = playerParent.transform.rotation.eulerAngles.y;
cameraRotationX = Camera.main.transform.rotation.eulerAngles.x;
playerRotationY += Input.GetAxisRaw("$$anonymous$$ouse X") * GameConstants.movementCameraSensitivity * Time.deltaTime;
cameraRotationX -= Input.GetAxisRaw("$$anonymous$$ouse Y") * GameConstants.movementCameraSensitivity * Time.deltaTime;
playerParent.transform.localEulerAngles = new Vector3(0, playerRotationY % 360, 0);
Camera.main.transform.localEulerAngles = new Vector3(cameraRotationX % 360, playerRotationY % 360, 0);
Camera.main.transform.position = playerParent.transform.position;
Camera.main.transform.Translate(Vector3.back * 6);
}
Your answer
Follow this Question
Related Questions
Third Person Controller Movement on Elevated Terrain 1 Answer
Moving a rotated plane in x+z directions with WASD? (bizarre behavior) 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to disable A, S, and D keys for third person player movement 1 Answer
Player walks to the right? 1 Answer