Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Understanding Code

Discussion in 'Scripting' started by parkerjrox, Sep 24, 2020.

  1. parkerjrox

    parkerjrox

    Joined:
    Sep 24, 2020
    Posts:
    1
    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<Animator>();
    _rigidBody = GetComponent<Rigidbody>();
    }

    // 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);
    }
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Welcome! Let me offer you a tip for code posting to help it be readable:

    Please use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    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.
     
    Bunny83 likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,446
    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.
     
    Kurt-Dekker likes this.