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
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: Unity - Scripting API: CharacterController.Move
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.
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
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.