Search
  • Charles Low

Hello Unity!

Updated: Oct 23

This blog will be my learning journal for me to refer to and track my progress. It probably won't contain anything creative or profound.



Populate scene with objects.

Familiarize with Interface and navigation in 3D viewport.

F Key to focus on object selected.

Understand terms:

  • Transform components

  • Inspector

  • Manipulate axis

Manipulate camera

Focus on objects

Use Flythrough view

Create scripts

IDE = Integrated Development Environment.

Debug.Log("Print string");

// Move object forward.

//transform = get component called "transform".

//.Translate = method. Capital T because it is a method, i.e. Transform().

//We want to change the position of the object using its tranform property. Vector3.forward = (0, 0, 1).

//Vector3 is a parameter that we are using for the Translate method.

transform.Translate(Vector3.forward)


//Above currently moves up the z-axis at 1m per frame. To fix speed to time, rather than frame, use Time.deltaTime.

transform.Translate(Vector3.forward * Time.deltaTime);

// Above means get the transform component and change/Translate the transform parameters (which, in the case of the transform component, is a Vector3 parameter (i.e. 3 vectors, 3 axis) by 1 unit, but multiplied by time, i.e. 1 unit per second on the z-axis.

___

Understand Rigidbody and Collider components.

___

//Create variable for speed

public float speed;

//The above means we are creating a public ("public" being the access modifier) variable (will appear in interface) that is a float (a number that will have a decimal place), called "speed".

//Access modifiers:

  • public - Show up in inspector and accessible by other scripts

  • [SerialiseField] private - Show up in inspector, not accessible by other scripts

  • [HideInInspector] public - Doesn't show in inspector, accessible by other scripts

  • private - Doesn't show in inspector, not accessible by other scripts

//When you create a variable but also assign it a default value, you are "initializing" the variable. E.g.:

// Note "f" after the initialized value of 5.0 basically indicates that it is a float.

public float speed = 5.0f;

___

Copy component values from play mode and implementing them outside of play mode.

___

//Create new script for camera to follow player with offset variable

// gameObject, like Vector3 or float or int, is a type/class

public gameObject player;
public Vector3 offset = new Vector3(0, 7, -13);
<...>
void Update()
{
transform.position = player.transform.position + offset;
}

// The above basically takes the current object the script is being applied to (i.e. the Camera) and makes its .position based on the transform component to be the same as the transform.position of the player plus an offset (i.e. the offset value is a Vector3 value that allows the camera to be placed aroudn the player but not directly on the player), e.g. 3P view.

___

// Change Update() method to LateUpdate() so that the update occurs after the Update() method for the movement happens (in this context, the Update() method is the movement of hte object forward and the script with LateUPpdate() will be for the camera following the player). The result is that the camera followint the player updates last (and the separate script of object movement occurs first).

___

//Create input for moving object left and right.
//Declare variable for x-axis input;
public float horizontalInput;

<...>

void Update()
{
//Assign input value to variable
horizontalInput = Input.GetAxis("Horizontal");

//Add variable to x-axis Translate method

transform.Translate(Vector3.right * Time.deltaTime * turnSpeed * horizontalInput);

// The above means that we are translating/changing the transform component of the object by a movement to the right/left on the x axis. We are also multiplying the extent of the movement by time rather than frame updates. Then we are multiplying it by a speed variable and also multiplying it by the horizontalInput variable which has been assigned input based on the Horizontal axis input from the Input manager. A similar script can be used for forward and backwards movements:

forwardInput = Input.GetAxis("Vertical");
<...>
transform.Translate(Vector3.right * Time.deltaTime * speed * forwardInput);

}

// Instead of an object, e.g. a car sliding left and right, you might want it to turn (like a proper car). To do this, instead of a transform.Translate, use tranform.Rotate. I.e.:

transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed * horizontalInput);

// Note that the above uses Vector3.up (as opposed to Vector3.right), because we are identifying the axis on which the rotations will be made. I.e. Vector3.up refers to the y-axis, and turns on the y-axis are turns to the left and right (rather than up and down). So, transform component, to Rotate on the parameters beign the Y-axis, at a rate based on time multiplied by the turnSpeed variable subject to the horizontalInput variable.

___

Toggle global or local (object) axis view on Unity editor.

___


FULL SCRIPT OF THIS EXERCISE (VEHICLE MOVEMENT)

==============================================

public class PlayerController : MonoBehaviour
{

    // Speed of vehicle
    private float speed = 25.0f;

    private float turnSpeed = 40.0f;

    // Input variables for turning left and right and moving forward and back
    private float horizontalInput;
    private float forwardInput;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

        // This is where we get player input
        horizontalInput = Input.GetAxis("Horizontal");
        forwardInput = Input.GetAxis("Vertical");

        // This is a comment, and this comment is telling users that Debug.Log method below will print "Messaeg 	Comment!" in the console.
        Debug.Log("Message Comment!");

        // This will make the vehicle move forward.
        transform.Translate(Vector3.forward * Time.deltaTime * speed * forwardInput);

        // This will make teh vehicle turn left or right.
        transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed * horizontalInput);

    }
}



public class CameraFollowPlayer : MonoBehaviour
{
    public GameObject player;
    [SerializeField] Vector3 offset = new Vector3(0, 3, -5);

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void LateUpdate()
    {
        transform.position = player.transform.position + offset;
    }
}





CHALLENGE 1

============

Using similar scripts as the exercise above, I fixed bugs in the following plane simulation exercise.


The main difference is in the script relating to the rotation of the plane - on the x-axis (and thus using Vector3.right or Vector3.left) rather than the y-axis (using Vector3.up):

public class PlayerControllerX : MonoBehaviour
{
    public float speed = 20.0f;
    public float rotationSpeed = 20.0f;
    public float verticalInput;

    // Start is called before the first frame update
    void Start()
    {
    }
    // Update is called once per frame
    void FixedUpdate()
    {
        // get the user's vertical input
        verticalInput = Input.GetAxis("Vertical");

        // move the plane forward at a constant rate
        transform.Translate(Vector3.forward * Time.deltaTime * speed);

        // tilt the plane up/down based on up/down arrow keys
        transform.Rotate(Vector3.left * rotationSpeed * Time.deltaTime * verticalInput);
    }
}

Also added a script to make the propeller spin:


public class PropellerSpin : MonoBehaviour
{
    public float rotateSpeed = 30.0f;
    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(Vector3.forward * Time.deltaTime * rotateSpeed);
    }
}





12 views0 comments

Recent Posts

See All