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;
// 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”);
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.
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, a simple introduction here) I could imagine you have some issues which those two lines?
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.