# Understanding Code

**URL:** <https://discussions.unity.com/t/understanding-code/810058>\
**Category:** Animation\
**Tags:** Animation, Scripting\
**Created:** [September 24, 2020, 8:49pm UTC](https://discussions.unity.com/t/understanding-code/810058 "2020-09-24T20:49:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![parkerjrox](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@parkerjrox](https://discussions.unity.com/u/parkerjrox)\
**Post date:** [September 24, 2020, 8:49pm UTC](https://discussions.unity.com/t/understanding-code/810058/1 "2020-09-24T20:49:06Z")

</div>

Hey guys this is my first post.

I’m new to both Unity and C#, so I don’t understand a lot of what is going on. (I am proficient in Java and have done a little of Python) My teacher had us code this in class, but I don’t understand a whole lot of what’s happening. If you guys could explain the code or point me to somewhere that could that would be great! I copied it in below, it is for making a character move and rotate with the WASD keys.

public class PlayerMovement : MonoBehaviour  
{  
public float turnSpeed = 100f;  
public float walkSpeed = 5f;

private Rigidbody \_rigidBody;  
private Vector3 \_movement;  
private Vector3 \_rotation;  
private Animator \_animator;  
private bool \_hasVerticalInput;  
private bool \_hasHorizontalInput;

// Start is called before the first frame update  
void Start()  
{  
\_animator = GetComponent();  
\_rigidBody = GetComponent();  
}

// Update is called once per frame  
void FixedUpdate()  
{  
// Get Horizontal and Vertical inputs  
float horizontal = Input.GetAxis(“Horizontal”);  
float vertical = Input.GetAxis(“Vertical”);

\_hasHorizontalInput = !Mathf.Approximately(horizontal, 0f);  
\_hasVerticalInput = !Mathf.Approximately(vertical, 0f);

if (\_hasVerticalInput)  
{  
// gets forward or backward direction  
if (vertical \> 0f)  
{  
\_movement = transform.forward;  
}  
else  
{  
\_movement = -transform.forward;  
}  
}

if (\_hasHorizontalInput)  
{  
// gets rotation position  
if (horizontal \> 0f)  
{  
\_rotation = Vector3.up;  
}  
else  
{  
\_rotation = Vector3.down;  
}  
}  
}

private void OnAnimatorMove()  
{  
if (\_hasHorizontalInput)  
{  
Quaternion deltaRotation = Quaternion.Euler(\_rotation \* turnSpeed \* Time.deltaTime);  
\_rigidBody.MoveRotation(\_rigidBody.rotation \* deltaRotation);  
}

if (\_hasVerticalInput)  
{  
\_rigidBody.MovePosition(\_rigidBody.position + \_movement \* walkSpeed \* Time.deltaTime);  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![Kurt-Dekker](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kurt-dekker/32/101578_2.png) [@Kurt-Dekker](https://discussions.unity.com/u/Kurt-Dekker)\
**Post date:** [September 24, 2020, 8:51pm UTC](https://discussions.unity.com/t/understanding-code/810058/2 "2020-09-24T20:51:11Z")

</div>

Welcome! Let me offer you a tip for code posting to help it be readable:

Please use code tags: [Using code tags properly](https://discussions.unity.com/t/481379)

Now that said, it looks pretty straightforward some kind of character controller. Do you have specific questions about the API? Or the individual calls? You can look all of them up, such as googling “Unity rigidbody” for example, and learn quite a bit about it from there.

---

<div class="post-metadata">

**Author:** ![Bunny83](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@Bunny83](https://discussions.unity.com/u/Bunny83)\
**Post date:** [September 25, 2020, 12:19am UTC](https://discussions.unity.com/t/understanding-code/810058/3 "2020-09-25T00:19:46Z")

</div>

> [@Kurt-Dekker](#):
>
> Now that said, it looks pretty straightforward some kind of character controller

Right. Also Java and C# have a lot in common. I would say something around 70 to 80% of the syntax is the same or quite similar.

@parkerjrox  
So since you said you are “proficient” in Java you shouldn’t have much trouble understanding the code from a language / syntax point of view. So I also don’t quite understand with which part you have trouble with. The code even has a lot of comments which I would consider useless or redundant.

Of course apart from pure syntax and language, games programming is mainly about logic and math, a lot of math.  
If you’re not familiar with quaternions ([also just math](https://en.wikipedia.org/wiki/Quaternion), a simple [introduction here](https://www.youtube.com/watch?v=3BR8tK-LuB0)) I could imagine you have some issues which those two lines?

> [@parkerjrox](#):
>
> Quaternion deltaRotation = Quaternion.Euler(\_rotation \* turnSpeed \* Time.deltaTime);  
> \_rigidBody.MoveRotation(\_rigidBody.rotation \* deltaRotation);

It simply creates a relative rotation called deltaRotation which represents a rotation around the global y axis (either clockwise or counter clockwise depending on what is stored in \_rotation). Since it’s multiplied by deltaTime the rotation amount “turnSpeed” is essentially given in degrees per second. So if we apply this relative rotation each frame and say turnSpeed is 30, it means that after 1 second the object has turned 30°. The second line simply rotates the rigidbody to a new orientation. The orientation we pass into MoveRotation is the old / current rotation of the rigidbody with the additional deltaRotation applied.

If you have trouble with anything else in the code I can tell you the most things in game development won’t get any easier than this and you probably should talk to your teacher.with your specific issues.

I just like to add, don’t be scared by quaternions. For the most part you don’t really need to understand them in depth, just how to use them.
