Search Unity

How do I get a GameObject to move left and up at the same time?

Discussion in 'Getting Started' started by BananaButcher, Jan 2, 2023.

  1. BananaButcher

    BananaButcher

    Joined:
    Jan 2, 2023
    Posts:
    7
    I have only been on Unity for 3 or 4 days, so feel free to talk to me like I had my brain removed.
     
  2. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    1. You need to use the Translate method (https://docs.unity3d.com/ScriptReference/Transform.Translate.html).
    2. The direction of movement is determined by Vector3 (https://docs.unity3d.com/ScriptReference/Vector3.html).
    3. Vector3 has some predefined values that are quite readable. Example below.
    Code (CSharp):
    1. // Both options are equal
    2. transform.Translate((Vector3.left + Vector3.up)); // Option 1: more readable
    3. transform.Translate(new Vector3(-1, 1, 0)); // Option 2: magic mumbo jumbo
    Of course, the example is not optimal, because you need deltaTime and speed.
    Code (CSharp):
    1. transform.Translate((Vector3.left + Vector3.up) * Time.deltaTime * 0.1f); // To slow down
    1. Beginner Scripting: https://learn.unity.com/project/beginner-gameplay-scripting
    2. Intermediate Scripting: https://learn.unity.com/project/intermediate-gameplay-scripting
    3. Junior Programmer Pathway: https://learn.unity.com/pathway/junior-programmer
    4. Manual: https://docs.unity3d.com/Manual/index.html
     
    Last edited: Jan 3, 2023