Methods for Moving Unity Characters and Their Differences
All Unity Character Movement Systems Methods
Hello friends, in this article I will tell you about the commands that allow you to move characters and objects in Unity.
I will also try to explain a few small details, such as which command should be used in what situations, happy reading.
Input Manager
The Input Manager is a detection system that gives us user inputs. Instead of writing code individually for devices such as keyboard or game controller to detect them, using the Input Manager allows us to detect them more efficiently.
Input Manager is automatically included in every project, but if you want to make changes to it, you can customize it as you wish from the Edit > Project Settings > Input Manager section within Unity.
Understanding Directions
First, before getting to all the commands, I will explain how we can determine the direction from the user using two different methods.
1) Checking Keyboard Keys
This is one of the methods I do not recommend at all, and after detecting the pressed key from the keyboard using if conditions, the desired operation/command is executed.
if(Input.GetKey(KeyCode.A)){
command
}
if(Input.GetKey(KeyCode.D)){
command
}
if(Input.GetKey(KeyCode.W)){
command
}
if(Input.GetKey(KeyCode.S)){
command
}Since it is clear which direction will be taken when which key is pressed with the above command, we can immediately write the direction codes after the if part, and our character moves in that direction.
float speed = 10;
if(Input.GetKey(KeyCode.A)){
transform.Translate(speed * Time.deltaTime,0,0);
}
if(Input.GetKey(KeyCode.D)){
transform.Translate(-speed * Time.deltaTime,0,0);
}
if(Input.GetKey(KeyCode.W)){
transform.Translate(0,0,speed* Time.deltaTime);
}
if(Input.GetKey(KeyCode.S)){
transform.Translate(0,0,-speed* Time.deltaTime);
}
I think the process above contains a bit too many unnecessary lines of code, so I consider this method an unnecessary way.
2) Using Input Manager
This is a truly useful method and with relatively few lines of code, we can get very efficient results.
A Vector3 variable is defined and the data from the Input Manager is placed inside. If Input Manager detects a predefined key is pressed, it gives values like +1 or -1 to represent direction, and you just have to multiply this value by the speed to make your character move.
Vector3 moveDirections;
moveDirections = new Vector3(Input.GetAxis("Horizontal"), 0 , Input.GetAxis("Vertical"));
transform.Translate(moveDirections.x * speed * Time.deltaTime, 0, moveDirections.z * speed * Time.deltaTime);
I defined a variable called speed as public, and later I can change this speed in my scene to find the appropriate speed value for my character.
According to the key information from the Input Manager, the x and z values of the Vector3 variable change to +1 or -1, and when you multiply it by speed, you get movement.
Horizontal represents the horizontal (right and left arrow keys, a and d keys) direction.
Vertical represents the vertical (up and down arrow keys, w and s keys) direction.
Methods for Moving
Moving with the Translate Method
Vector3 moveDirections;
moveDirections = new Vector3(Input.GetAxis("Horizontal"), 0 , Input.GetAxis("Vertical"));
transform.Translate(moveDirections.x * speed * Time.deltaTime, 0, moveDirections.z * speed * Time.deltaTime);
Moving Using Position
Vector3 moveDirections;
moveDirections = new Vector3(Input.GetAxis("Horizontal"), 0 , Input.GetAxis("Vertical"));
transform.position = new Vector3(transform.position.x + moveDirections.x, transform.position.y , transform.position.z + moveDirections.z);
Moving Using AddForce
Vector3 moveDirections;
moveDirections = new Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0 , Input.GetAxis("Vertical") * speed * Time.deltaTime);
characterRB.AddForce(moveDirections, ForceMode.Impulse);
Moving Using Velocity
Vector3 moveDirections;
moveDirections = new Vector3(Input.GetAxis("Horizontal") * speed, 0 , Input.GetAxis("Vertical") * speed);
characterRB.velocity = moveDirections;



Yorum Gönder