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

Question How do I make the player rotate towards the moving direction?

Discussion in 'Editor & General Support' started by JussiLesonen, Jun 16, 2023.

  1. JussiLesonen

    JussiLesonen

    Joined:
    Oct 21, 2020
    Posts:
    3


    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class TestOrbit : MonoBehaviour
    7. {
    8.     public GameObject asteroid;
    9.     public GameObject player;
    10.     public GameObject rayFollow;
    11.     public GameObject offset;
    12.     public GameObject offsetFollow;
    13.     public GameObject parent;
    14.  
    15.     public Camera cam;
    16.  
    17.     RaycastHit hit;
    18.     RaycastHit followHit;
    19.  
    20.     private void FixedUpdate()
    21.     {
    22.         Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    23.  
    24.         LayerMask layerMask = ~((1 << 7) | (1 << 8) | (1 << 9));
    25.  
    26.         Physics.Raycast(ray, out hit, 50, layerMask);
    27.  
    28.         Debug.DrawLine(ray.origin, hit.point, Color.green);
    29.  
    30.         rayFollow.transform.position = hit.point;
    31.  
    32.         offsetFollow.transform.LookAt(transform);
    33.  
    34.         if (Vector3.Distance(offset.transform.position, offsetFollow.transform.position) > 1f)
    35.         {
    36.             offsetFollow.transform.position = Vector3.Lerp(offsetFollow.transform.position, offset.transform.position, 2f * Time.deltaTime);
    37.         }
    38.  
    39.         Vector3 fwd = offsetFollow.transform.TransformDirection(Vector3.forward);
    40.  
    41.         Debug.DrawRay(offsetFollow.transform.position, fwd * 50, Color.blue);
    42.  
    43.         Physics.Raycast(offsetFollow.transform.position, fwd, out followHit, 50, layerMask);
    44.  
    45.         // Parent object is set to the raycast hitpoint
    46.         parent.transform.position = followHit.point;
    47.         parent.transform.rotation = Quaternion.Lerp(parent.transform.rotation, Quaternion.FromToRotation(Vector3.up, followHit.normal), 3f * Time.deltaTime);
    48.  
    49.         // Player follows the parent but has a separate look rotation towards the movement direction
    50.         player.transform.LookAt(rayFollow.transform);
    51.         player.transform.localEulerAngles = new Vector3(0, player.transform.localEulerAngles.y, 0);
    52.  
    53.         // Here I rotate the camera
    54.         if (Input.GetKey(KeyCode.W))
    55.         {
    56.             transform.Rotate(Vector3.right * 40f * Time.deltaTime);
    57.         }
    58.  
    59.         if (Input.GetKey(KeyCode.S))
    60.         {
    61.             transform.Rotate(Vector3.left * 40f * Time.deltaTime);
    62.         }
    63.  
    64.         if (Input.GetKey(KeyCode.A))
    65.         {
    66.             transform.Rotate(Vector3.up * 40f * Time.deltaTime);
    67.         }
    68.  
    69.         if (Input.GetKey(KeyCode.D))
    70.         {
    71.             transform.Rotate(Vector3.down * 40f * Time.deltaTime);
    72.         }
    73.     }
    74. }
    75.  
    76.  
     
    Last edited: Jun 18, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Here's how to do it:

    You may be able to simply drive
    transform.forward
    (or
    transform.up
    when in 2D) equal to your movement vector.

    Otherwise, use Mathf.Atan2() to derive heading:

    https://forum.unity.com/threads/yaxis-rotation-parent-object.796419/#post-5299722

    Sine/Cosine/Atan2 (sin/cos/atan2) and rotational familiarity and conventions

    https://forum.unity.com/threads/vector2-from-an-angle.1203448/#post-7690462
     
  3. JussiLesonen

    JussiLesonen

    Joined:
    Oct 21, 2020
    Posts:
    3
    I solved the problem by myself but I can't figure out how to delete this thread
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    You are not supposed to delete that. The entire #%#^#$ point of writing posts in the forum is for everybody to benefit, not just for you to skulk in, get your information, skulk back out and destroy everything in your wake.

    Don't do that, don't be so selfish. Your post has been reported in order to get it restored properly.
     
  5. JussiLesonen

    JussiLesonen

    Joined:
    Oct 21, 2020
    Posts:
    3
    Sorry, still new to posting on this forum. I put the code back and presented how I solved the problem.