Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Brackeys 3rd person controller

Discussion in 'Getting Started' started by AlexWorld_A, Mar 27, 2024.

  1. AlexWorld_A

    AlexWorld_A

    Joined:
    Mar 31, 2023
    Posts:
    3
    hey guys, I tried to make 3d top-down shooter game with brackeys 3rd person controller and movement works pretty good but then I tried to make player rotation on y-axis follow the mouse I tried all the methods I know but it didn't work does anybody can help?
     
  2. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,997
    without seeing the code etc - no not really. People are happy to help with minimal info provided, but no info means we would need to be mind readers
     
  3. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,976
    Also Brackeys has been out of the unity world i thought for a while, there are other 3rd person controllers (including unitys) to look into
     
  4. developer3244

    developer3244

    Joined:
    Jan 4, 2024
    Posts:
    102
  5. AlexWorld_A

    AlexWorld_A

    Joined:
    Mar 31, 2023
    Posts:
    3
    okay here the code
    using UnityEngine;
    public class PlayerMovement : MonoBehaviour
    {
    public CharacterController controller;
    public float speed = 6f;
    public Camera cam;
    void Update()
    {
    float horizontal = Input.GetAxisRaw("Horizontal");
    float vertical = Input.GetAxisRaw("Vertical");
    Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
    //mouse pos
    Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, out RaycastHit raycastHit))
    {
    transform.rotation = Quaternion.Euler(0f, raycastHit.normal.y, 0f);
    }
    if (direction.magnitude >= 0.1f)
    {
    controller.Move(direction * speed * Time.deltaTime);
    }
    }
    }
     
    Last edited: Mar 30, 2024
  6. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,976
    other than for the love of all things holy use codetags

    you arent rotating around y you are potentially rotating around x and z at the same time. chaos is likely
     
  7. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,054
    Code (CSharp):
    1. using UnityEngine;
    2. public class PlayerMovement : MonoBehaviour
    3. {
    4. public CharacterController controller;
    5. public float speed = 6f;
    6. public Camera cam;
    7.     void Update()
    8.     {
    9.         float horizontal = Input.GetAxisRaw("Horizontal");
    10.         float vertical = Input.GetAxisRaw("Vertical");
    11.         Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
    12.         //mouse pos
    13.         Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    14.         if (Physics.Raycast(ray, out RaycastHit hit))
    15.         {
    16.             transform.rotation=Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(hit.point-transform.position),5*Time.deltaTime); // smoothly look towards the hit point
    17.         }
    18.         if (direction.magnitude >= 0.1f)
    19.         {
    20.             controller.Move(direction * speed * Time.deltaTime);
    21.         }
    22.     }
    23. }
     
  8. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,446
    Your code is missing most of the code he shows in his tutorial video. Are you using a different tutorial or a different source for the code?

     
  9. AlexWorld_A

    AlexWorld_A

    Joined:
    Mar 31, 2023
    Posts:
    3
    Yeah but all he doing after is rotation by *MOVING* when I need to make rotation by mouse
     
    Last edited: Mar 30, 2024