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

Resolved Codemonkey Course: transform.forward strange movement?

Discussion in 'Scripting' started by Darkisolation, Aug 14, 2023.

  1. Darkisolation

    Darkisolation

    Joined:
    Jan 11, 2015
    Posts:
    4
    Hello Guys,

    currently I am learning unity with the course from codemonkey

    .

    Everything works fine until I get to the part of movement, exactly the rotation by the transform.forward method. If I remove the forward method everything works fine (without the rotation of the character).

    If I add transform.forward = moveDir; the character makes some strange movement when I press the movement keys.

    Code (CSharp):
    1.  
    2. private void Update()
    3. {
    4.      Vector2 inputVector = new Vector2 (0, 0);
    5.      if (Input.GetKey(KeyCode.W)) {
    6.           inputVector.y = +1:
    7.      }
    8.      if (Input.GetKey (KeyCode.s)) {
    9.           inputVector.y = -1:
    10.      }
    11.      if (Input.GetKey(KeyCode.A)) {
    12.           inputVector.x = -1;
    13.      }
    14.      if (Input.GetKey(KeyCode.D)) {
    15.           inputVector.x = +1:
    16.      }
    17.      inputVector = inputVector.normalized;
    18.      Vector3 moveDir = new Vector3(inputVector.x, 0f, inputVector.v):
    19.  
    20.      transform.position += moveDir * moveSpeed * Time.deltaTime:
    21.      transform.forward = moveDir:
    22.      Debug.Log(Time.deltaTime);
    23. }
    24.  
    25.  
    Has anybody an idea how to solve the problem? I got an info message from unity that I passed an empty Vector to the forward method.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,954
    they have slightly different code for that transform.forward? (at least at 1:22.22)
     
  3. Darkisolation

    Darkisolation

    Joined:
    Jan 11, 2015
    Posts:
    4
    Yes I know but it should work without the Slerp Method. As you can see in 1:20:37
     
  4. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    105
    It looks like you've mistakenly placed colons ( : ) at the end of most of your lines instead of semicolons ( ; ).
     
    wideeyenow_unity likes this.
  5. Darkisolation

    Darkisolation

    Joined:
    Jan 11, 2015
    Posts:
    4
    Thanks for your answer, I got a reply for codemonkey directly as a comment for this video. The problem was that the visual position offset from the character was not 0,0,0.
    After I fixed that, everything works fine.

    So problem solved.
     
  6. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    431
    I want to add to the above code that you have something like const values in Unity. You don't have to type +1 and -1, you can just use Vector3.left, right, forward, back or Vector2.left, right, up, down. Even more, you have some functions like Translate.
    Code (CSharp):
    1. // Move the object forward along its z axis 1 unit/second.
    2. transform.Translate(Vector3.forward * Time.deltaTime);
    More movement examples?
    Code (CSharp):
    1. // Move the object forward and backward along its z axis 1 unit/second. The player presses W it goes forward, the player presses S it goes back.
    2. transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * Time.deltaTime);
    https://learn.unity.com/pathway/junior-programmer
     
    Last edited: Aug 14, 2023
  7. Darkisolation

    Darkisolation

    Joined:
    Jan 11, 2015
    Posts:
    4
    Thank you for the examples :). The code above is only copied from the course video, it's not my code and later in the course there will be code refactoring.

    I'm a professional programmer, so coding is not the problem, more the creative part :D