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:
For a simpler movement, maybe useful in other games, you could set the position of the transform directly:
void Update ()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
float speed = 5.0f;
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 a new script named PlayerMovement.cs, fill it using this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent( typeof(CharacterController) )]
public class PlayerMovement : MonoBehaviour
{
[SerializeField] float _speed = 25.0f;
[SerializeField] float _jumpSpeed = 8.0f;
[SerializeField] float _gravity = 20.0f;
[SerializeField] float _sensitivity = 5f;
CharacterController _controller;
float _horizontal, _vertical;
float _mouseX, _mouseY;
bool _jump;
// use this for initialization
void Awake ()
{
_controller = GetComponent<CharacterController>();
}
// screen drawing update - read inputs here
void Update ()
{
_horizontal = Input.GetAxis("Horizontal");
_vertical = Input.GetAxis("Vertical");
_mouseX = Input.GetAxis("Mouse X");
_mouseY = Input.GetAxis("Mouse Y");
_jump = Input.GetButton("Jump");
}
// physics simulation update - apply physics forces here
void FixedUpdate ()
{
Vector3 moveDirection = Vector3.zero;
// is the controller on the ground?
if( _controller.isGrounded )
{
// feed moveDirection with input.
moveDirection = new Vector3( _horizontal , 0 , _vertical );
moveDirection = transform.TransformDirection( moveDirection );
// multiply it by speed.
moveDirection *= _speed;
// jumping
if( _jump )
moveDirection.y = _jumpSpeed;
}
float turner = _mouseX * _sensitivity;
if( turner!=0 )
{
// action on mouse moving right
transform.eulerAngles += new Vector3( 0 , turner , 0 );
}
float looker = -_mouseY * _sensitivity;
if( looker!=0 )
{
// action on mouse moving right
transform.eulerAngles += new Vector3( looker , 0 , 0 );
}
// apply gravity to the controller
moveDirection.y -= _gravity * Time.deltaTime;
// make the character move
_controller.Move( moveDirection * Time.deltaTime );
}
}
And then drag it to Player.
You can also setting the speed, sensitivity, jump speed and gravity.
Super quick and informative explainer here:
Don’t forget Rigidbody controllers as well.
By locking rotation of the Rigidbody on all axis in the editor (so it doesn’t fall over), you can use AddForce() on the transform.forward/right/left/back, depending on a button press. Also AddForce upwards for jumping.
@BigBoss8281
Your code is so strange in my game. the player moves with negative gravity, then if you move the cursor it starts rotating.