In this video you will learn how to make RPG game:
– Terrains
– Player Animation (Idle, Running).
– Joystick movement (Player movement and rotation)
Player movement:
In this part of unity tutorial, we will user this free package of Soldier from Unity assets store.
Let’s add “PlayerMovement.cs” script attached to Soldier Prefab:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Vector3 playerMovement;
private Quaternion PlayerQuaternion;
private Animator animator;
private Rigidbody rigidbody;
private float fireCountDown = 0f;
private float fireRate = 3f;
public FixedJoystick LeftJoystick;
public FixedJoystick RightJoystick;
public float Speed = 100f;
public GameObject CapsulePrefab;
public GameObject FirePoint;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
rigidbody = GetComponent<Rigidbody>();
PlayerQuaternion = new Quaternion();
}
// Update is called once per frame
void Update()
{
MovePlayer(LeftJoystick.Direction.x, LeftJoystick.Direction.y);
}
private void MovePlayer(float x, float z)
{
playerMovement.Set(x, 0f, z);
playerMovement = playerMovement * Speed * Time.deltaTime;
if (LeftJoystick.IsPressed)
{
var turn = new Vector3(LeftJoystick.Direction.x, 0f, LeftJoystick.Direction.y);
PlayerQuaternion = Quaternion.LookRotation(turn);
}
else
{
PlayerQuaternion = transform.rotation;
}
transform.SetPositionAndRotation(transform.position + playerMovement, PlayerQuaternion);
if(transform.position - playerMovement != transform.position)
{
animator.SetBool("IsRuning", true);
}
else
{
animator.SetBool("IsRuning", false);
}
}
}
In “Update” method, we used “MovePlayer” method .
MovePlayer method:
In this method, we manage the player’s movement behavior. So it’s takes two parameters , the “LeftJoystick.Direction.x” on x axis and LeftJoystick.Direction.y on z axis.
Before updating the player’s position, you need to check whether the Left joystick is pressed.
After that, calculate the right player’s movement direction by this lines of code:
var turn = new Vector3(LeftJoystick.Direction.x, 0f, LeftJoystick.Direction.y);
PlayerQuaternion = Quaternion.LookRotation(turn);
Finally, update the player movement and direction.
transform.SetPositionAndRotation(transform.position + playerMovement, PlayerQuaternion);
Player Animation:
In the “Start” method, initialize the animator property, it’s used to handle transitions between the different animation stats (Idle, running, shooting) :
animator = GetComponent<Animator>();
In “MovePlayer” method, we need only to sets the rules to pass to the right stats of animation. Check if player is moving, so we set the condition to pass to “running” state to true else to false and the player by default is in “Idle” state.
if(transform.position - playerMovement != transform.position)
{
animator.SetBool("IsRuning", true);
}
else
{
animator.SetBool("IsRuning", false);
}
}
Follow Me For Updates
Subscribe to my YouTube channel or follow me on Twitter or GitHub to be notified when I post new content.