- Home /
Character Controller
Hello everyone!
I'm relatively new to Unity and I've been trying to learn as much as I can about scripting through YouTube but most people seem to have their own way of doing things, so it's difficult to add the the things I've learned together, so I thought I'd come here.
What I'm hoping to do is make a first-person character controller in C#. The game I'm making makes it difficult to just use the ones found in the Standard Assets package because I seem to need multiple scripts just to do simple things and it just feels sloppy. I would like to have a "Leg" object that controls forward/backward/turning movement, a "Torso" object that rotates on the y-axis, and a "Head/Camera" object that rotates on the x-axis.
What I'm guessing I need to do is reference the separate game objects in the script so that the "Torso" and "Head" stay "connected" to each other, so that the "Torso" and "Head" rotates relative to the "Legs" and "Torso," respectively, and so that the "Head" faces the same x-direction as the "Torso" at all times, but the "Torso" doesn't rotate along the x-axis with the "Head." The best way to describe it would be a tank, with a separate chassis, turret, and barrel.
I'm also unsure about syntax. I know I'll need transform.Translate, transform.Rotate, Vector3 (which controls direction?), and Time.deltaTime. I'm not sure how to add a sprint or crouch feature; is there a command to only increase speed when a key is being pressed and a command to toggle speed on/off, and could this also work to toggle the camera's FOV for an aiming-down-sights mode? I'd also like to add a headbob feature so I would transform the camera's position using cos(y) and sin(x)?
Thanks for any help in advance. I don't expect anyone to write the script for me, I'd just like an overview of any commands I might need and the syntax required to run it in Unity.
Answer by killerstreak · Dec 28, 2015 at 05:54 PM
not sure what to say about the whole controls thing but about the other questions, A few suggestions:
Rigidbody.velocity = new Vector3(etc..) is great for moving
You could use a if statement to check whether a key is pressed, if so increase velocity
You can manipulate the FOV easily because its a element of the camera that is accessible by script eg: Camera.Main.fieldOfView = setittowhateveryoulike activated by also an if statement that checks whether a button si pressed.
Vectors are a manner of expressing direction and speed a Vector3 takes 3 arguments an xyz coordinate, the combined value of these form a direction. eg (1,0,0) means only a velocity in positive x direction so basically from left to right.
You should look up how exactly vectors work though, and how to use math on them.
So I could change the velocity forward and back using Rigidbody.velocity, and then use transform.rotate to change the direction?
Could the if statement toggle between sprinting and running or will it only sprint while the key is being pressed?
So if I have multiple cameras (ie a $$anonymous$$imap and a first-person camera) the Camera.$$anonymous$$ain.fieldOfView could be changed to select a specific camera?
Along the same lines, can most commands be applied to certain objects? Say, if my script is on an empty object with 2 cubes and I only want one to move when I press a key, can I state that somewhere?
Thanks for the help!
not really, transform.rotate rotates the object, but a vector is a direction that is represented by world coordinates, not relative to object orientation, you will have to use a bit of math to calculate the new vector that accompanies said rotation.
(maybe you should look into if statements and how exactly they work, they are super important, just so you can answer those questions yourself :).) e.g.
int state = 1; if(Input.GetButtonDown($$anonymous$$eyCode.shift) { //sprint } else if(Input.GetButtonDown($$anonymous$$eyCode.ctrl) { //crouch } else if(Input.GetButtonDown($$anonymous$$eyCode.caps) { state = state * -1; } else { if(state == 1) { //run } else { //walk } }
this code checks if a certain key is pressed and reacts accordingly, there is also a toggle mod where pressing the caps will toggle the int state between the values 1 and -1 this is then again checked when the code doesnt detect user input, and the code decides whether you are walking or running.
camera.$$anonymous$$ain references the $$anonymous$$ain camera, its a shortcut but you can certainly use another camera, you could reference it like this.
Camera yourcamera = GameObject.Find("camera").getComponent<Camera>(); yourcamera.fieldOfView = //etc;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to make object move where the camera points? 1 Answer
C# CharacterController.SimpleMove Goes on Forever 1 Answer
Character movement to set positions 1 Answer