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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Character does not move where camera is facing

Discussion in 'Scripting' started by UnityAcct1625, Jan 24, 2023.

  1. UnityAcct1625

    UnityAcct1625

    Joined:
    Sep 28, 2022
    Posts:
    6
    Hey everyone, i made a character controller script and i've been trying to make the player turn and move where the camera is facing but for some reason it doesn't work, here's the code

    Code (CSharp):
    1. using System.Collections;
    2.  
    3. using System.Collections.Generic;
    4.  
    5. using UnityEngine;
    6.  
    7.  
    8.  
    9. public class TPMovementFinal : MonoBehaviour
    10.  
    11. {
    12.  
    13.     public  CharacterController controller;
    14.     public Transform cam;
    15.     public float speed = 6f;
    16.     public float turnSmoothTime = 0.1f;
    17.     public float gravity = 100f;
    18.     float turnSmoothVelocity;
    19.  
    20.     // Update is called once per frame
    21.  
    22.     void Update()
    23.  
    24.     {
    25.  
    26.         float horizontal = Input.GetAxisRaw("Horizontal");
    27.         float vertical = Input.GetAxisRaw("Vertical");
    28.         CharacterController controller = gameObject.GetComponent <CharacterController>();
    29.         Vector3 direction = new Vector3(horizontal, 0, vertical);
    30.  
    31.         //angle & movement
    32.         if (direction.magnitude >= 0.1f)
    33.  
    34.         {
    35.  
    36.         float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    37.         float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    38.         transform.rotation = Quaternion.Euler(0f,angle, 0f);
    39.  
    40.         Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    41.         controller.Move(moveDir.normalized * speed * Time.deltaTime);
    42.  
    43.         }
    44.          
    45.          // gravity
    46.          Vector3 vertDir = new Vector3(0f, 0f, 0f);
    47.          vertDir.y -= gravity * Time.deltaTime;
    48.          controller.Move(vertDir * Time.deltaTime);
    49.  
    50.     }
    51.  
    52. }
    The part that is supposed to make the character move towards the direction the camera is pointing at is this:
    Code (CSharp):
    1.  float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    2.         float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    3.         transform.rotation = Quaternion.Euler(0f,angle, 0f);
    4.  
    5.         Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    6.         controller.Move(moveDir.normalized * speed * Time.deltaTime);
     
  2. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    234
    That's a lot of unneeded work.

    You can just get forward and right from the camera transform, and multiply those by your vertical and horizontal values. :)

    Just make sure to set the Y from the cameras forward to 0 and then normalize it, to straighten out the vector in case the camera is pointing a bit downwards.
     
  3. UnityAcct1625

    UnityAcct1625

    Joined:
    Sep 28, 2022
    Posts:
    6
    So sorry for asking this (i'm a big noob), but how could i do that?
     
  4. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    234
    You already have your Camera transform.

    ----
    cam.right will get you the right direction of the camera. So multiplying it by your horizontal will essentially let you move left/right.

    -----
    cam.forward will get you the direction the camera is facing, so multiplying by your vertical will get that movement.

    only issue is if your camera is facing down slightly, your char will also try to move into the ground, thus you need to straighten out the forward vector like this:

    Vector3 forward = cam.forward;
    forward.z = 0;
    forward.Normalize();

    note im not home so I can't test, might need to set y to 0 instead.
     
    UnityAcct1625 likes this.