- Home /
How would I make a 3rd person Controller that only moves up, down, left, and right?
Hello,
I'm making a game where you control a cube as it goes through a line of obstacles on it's way. It would be in 3D and the obstacles are other cubes popping out of the wall and would kinda have a racing feel to it. Now, I need to know how to make a 3rd person controller for a cube so that it only can move up, down, left, and right. It would not have any gravity yet not constantly having it keep moving after you press the button. Also, the controls would be W A S D. Thanks for any help.
Answer by nixtwiz · Oct 06, 2012 at 10:57 PM
if you dont need any physics-based movement you could just use transform.position. That may cause some problems with collision though.
You could also use a rigidbody, set Use Gravity to false, make force applied only when the velocity is below the max speed (to limit speed), and then when the player is not pressing a button set the rigidbody's velocity to 0 so it doesn't keep moving when the button is no longer pressed with rigidbody.velocity = Vector3.zero.
Answer by TheDarkVoid · Oct 06, 2012 at 11:56 PM
I would recommend something like this, it uses a character controller and it goes in your `Update()` function:
CharacterController Controller = GetComponent<CharacterController>();
Vector3 dir = thisTransform.forward * curSpeed;
if(Input.GetKey(KeyCode.W) && curSpeed < HighSpeed)
curSpeed += Input.GetAxis("Vertical") * Acceleration * Time.deltaTime;
if(Input.GetKey(KeyCode.S) && curSpeed > LowSpeed)
curSpeed -= Input.GetAxis("Vertical") * Acceleration * Time.deltaTime;
if(!Controller.isGrounded)
dir.y -= Gravity * Time.deltaTime;
Controller.Move(dir * Time.smoothDeltaTime);
You will need to define curSpeed, Gravity, HighSpeed, LowSpeed, Acceleration, and you may want to put set the Controller in the `Start()` function.
Could you put the entire script in there. I'm having trouble with defining the stuff. Thanks.
Here is an even simpler script on the scripting ref: http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.$$anonymous$$ove.html
YOu will also need to put the smoothFollow script on your camera and target your player.
Your answer
![](https://koobas.hobune.stream/wayback/20220613081958im_/https://answers.unity.com/themes/thub/images/avi.jpg)