A bit confused about the parameter rotationThisFrame

Hello! I am very new to coding so please forgive me. First off, in unity, the code is working just fine. No issues there. But, I am having a hard time understanding it. I get a bit lost when we start to use the rotationThisFrame parameter in the ApplyRotation method. I understand the value of the variable rotationThrust because it is defined at the beginning of the code (currently set to 1). But I am not quite sure where the value of rotationThisFrame comes from. Any assistance would be greatly apprecaited. Thanks!

This is what I have right now:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour

{
Rigidbody rb;
[SerializeField] float mainThrust = 1f;
[SerializeField] float rotationThrust = 1f;

void Start()
{
    rb = GetComponent<Rigidbody>();
}

void Update()
{
    ProcessInput();
    ProcessRotation();
}

void ProcessInput()
{
    if (Input.GetKey(KeyCode.Space))
    {
        rb.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);
    }
}

void ProcessRotation()
{  
    if (Input.GetKey(KeyCode.A))
    {
        ApplyRotation(rotationThrust);
    }
    else if (Input.GetKey(KeyCode.D))
    {
        ApplyRotation(-rotationThrust);
    }
}

void ApplyRotation(float rotationThisFrame)
{
    transform.Rotate(Vector3.forward * rotationThisFrame * Time.deltaTime);
}

}

Hi @u4icfln! Welcome to the community

rotationThisFrame is a parameter to a function

void ApplyRotation(float rotationThisFrame /* <-- it's here */)
{
    transform.Rotate(Vector3.forward * rotationThisFrame * Time.deltaTime);
}

When ProcessRotation is running, it passes a value to ApplyRotation, and this value is named rotationThisFrame inside ApplyRotation

void ProcessRotation()
{  
    if (Input.GetKey(KeyCode.A))
    {
        ApplyRotation(rotationThrust); // <-- Here it passes rotationThrust
    }
    else if (Input.GetKey(KeyCode.D))
    {
        ApplyRotation(-rotationThrust); // <-- Here it passes -rotationThrust
    }
}

So, Depending on which key you pressed (A or D) the value of rotationThisFrame will be equal to either rotationThrust or -rotationThrust

1 Like

I just want to be sure I understand correctly.
When we DEFINE a method with a parameter we are expecting a value to be input into the method.
When CALLING a method we are outputting values.

So by DEFINING the method ApplyRotation with the parameter (float rotationThisFrame), we are expecting a value to be input to complete the ApplyRotation method. We have named this value “rotationThisFrame”.

When we CALL ApplyRotation we are outputting either “rotationThrust” or “-rotationThrust”. And the name of that output value, be it positive or negative, is “rotationThisFrame” because that is what we chose to name it when DEFINING ApplyRotation.

Is that right?

Your first two paragraphs are correct. The ApplyRotation method is defined with a parameter (float rotationThisFrame). When we call the ApplyRotation method, we must pass on a value of type float to it.

The method does not return anything because the return type is void. If the return type was, for example, int or string, the method would need the return keyword, and it would have to return an object of the defined type.

We do not output anything in the ApplyRotation method. We just use the parameter like a local variable.


See also:

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms