- Home /
How to make a 3D character move?
Howdy guys c: Very newbie question here, trying to teach myself unity. Made a stock character and gave him an idle and a walk animation for now, but I can't seem to figure out how to make him move with the WASD keys. My endgame goal is to be able to walk, jump, and maybe dodge roll if I can clear that hurdle, but for now my character is forever doing the idle shuffle. Is there a walkthrough that covers this that I missed, or does anyone have some advice? Been banging my head against my keyboard for awhile :s
Answer by metalted · Jun 21, 2019 at 03:06 PM
My advice is using a charactercontroller, because it is the least trouble for a beginner. The sample script on the unity docs already has a jump attached. So if you want to use it add a charactercontroller component to your object and and this script: https://docs.unity3d.com/ScriptReference/CharacterController.Move.html
For a simpler movement, maybe useful in other games, you could set the position of the transform directly:
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
float speed = 5.0f;
void Update(){
transform.position = new Vector3(horizontal, 0, vertical) * speed * Time.deltaTime;
}
So this way of moving is very very basic, but it will give you an idea of how you can retrieve axis input to control your character.
Thank you for the quick response c: I tried applying a character controller and pasted in the script that comes with it, but my player now floats about a person and a half up into the air and the controller doesn't seem to have an effect. I'm doing this stuff in an empty scene so I don't goof my level up. Sorry if I'm missing something obvious, the character controller script is giving me an error message too
Character and error message (all of his pieces have a skinned mesh renderer attached: https://cdn.discordapp.com/attachments/343449258168549386/591721159989526658/unknown.png
Plane settings: https://cdn.discordapp.com/attachments/343449258168549386/591711183892119591/unknown.png
Scene from gameplay: https://cdn.discordapp.com/attachments/343449258168549386/591711267685793872/unknown.png
Okay I see the problem here. The thing is, you need to make sure your name of your class matches the name of your file. The class is most likely not called CharacterController.$$anonymous$$ove, because I don't think dots are allowed in a class name. If you want to copy the complete code from the unity docs, you will have to call it ExampleClass, because in the script you see the following:
public class ExampleClass : $$anonymous$$onoBehaviour
The reason your character is floating in the air is because of the charactercontroller itself. The collider of the charactercontroller is nothing more than a capsule collider. So you have to make sure that the size of the collider matches the size of your character. You can change those setting using the size, center and radius parameters on the charactercontroller component in the inspector.
Thank you so much, that sorted those two issues out :D ... now I know that the walk animation isn't triggering xD Ah well, one headdesk at a time, I'll see if I can get this one sorted and if I can't then I'll open another thread. Thank you very much for your time, I'm slowly headbutting my way through this stuff. In the middle of making the start screen now c:
If you change the last line into transform.position += new Vector3(horizontal, 0, vertical) * speed * Time.deltaTime;
it works for more diverse situations, because you are hardsetting 'y' to 0 when you press any of the WASD buttons but with +=
you would only add the new Vector to your gameobject's position, thus never changing the 'y' axis. This would e. g. allow you to jump and still move while airborne.
Answer by BigBoss8281 · Nov 08, 2020 at 08:31 AM
CODE TO MAKE PLAYER MOVE MAKE NEW SCRIPT AND NAME IT playerMovement.cs AND COPY PASTE THIS SCRIPT:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour {
public float speed = 25.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
private float turner;
private float looker;
public float sensitivity = 5;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
CharacterController controller = GetComponent<CharacterController>();
// is the controller on the ground?
if (controller.isGrounded) {
//Feed moveDirection with input.
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
//Multiply it by speed.
moveDirection *= speed;
//Jumping
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
turner = Input.GetAxis ("Mouse X")* sensitivity;
looker = -Input.GetAxis ("Mouse Y")* sensitivity;
if(turner != 0){
//Code for action on mouse moving right
transform.eulerAngles += new Vector3 (0,turner,0);
}
if(looker != 0){
//Code for action on mouse moving right
transform.eulerAngles += new Vector3 (looker,0,0);
}
//Applying gravity to the controller
moveDirection.y -= gravity * Time.deltaTime;
//Making the character move
controller.Move(moveDirection * Time.deltaTime);
}
}
And then drag it to Player,You can also setting the speed,sensitivity,Jump and gravity
And don't forget to give character controller in your player
thank you sooooooooooooo much. i allways forgot to give character controller and i had many mistakes in the script
If i use this script my whole character moves in the camera direction and if move the character bounces and my camera freaksout
Answer by Llama_w_2Ls · Nov 22, 2020 at 04:05 PM
Dont forget Rigidbody controllers as well. By locking rotation of the rigidbody on all axis in the editor (so it doesnt fall over), you can use AddForce() on the transform.forward/right/left/back, depending on a button press. Also AddForce upwards for jumping.
Answer by Tarodev · May 27, 2021 at 07:28 AM
Super quick and informative explainer here: https://youtu.be/ELz_EG-s0jU