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 How To Move Relative to Camera and not World Axis

Discussion in 'Scripting' started by NameyMcNameFace, Oct 16, 2022.

  1. NameyMcNameFace

    NameyMcNameFace

    Joined:
    Oct 10, 2022
    Posts:
    12
    Hi,

    so really I'm just stumped. My understanding of code is rudimentary, but I've been able to get by with the help of tutorials and forums. That being said no matter what tutorial or forum I find I can't seem to get the player movement to move relative to where the camera is facing. If anyone has a better understanding and could educate me I would appreciate it.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NewPlatformController : MonoBehaviour
    6. {
    7.     public float speed = 10f;
    8.     public bool useGravity = true;
    9.     public float gravityModifier = 5f;
    10.     public float jumpHeight = 25f;
    11.     public float rotateSpeed = 500f;
    12.  
    13.     Rigidbody rb;
    14.  
    15.     void Start()
    16.     {
    17.         rb = GetComponent<Rigidbody>();
    18.     }
    19.  
    20.     void FixedUpdate()
    21.     {
    22.         GetComponent<Rigidbody>().useGravity = false;
    23.         if (useGravity) GetComponent<Rigidbody>().AddForce(Physics.gravity * (GetComponent<Rigidbody>().mass * GetComponent<Rigidbody>().mass) * gravityModifier);
    24.        
    25.         Vector3 m_Input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    26.  
    27.         rb.MovePosition(transform.position + m_Input * Time.deltaTime * speed);
    28.  
    29.         //This function rotates the player to face the direction they are traveling
    30.         if (m_Input != Vector3.zero)
    31.         {
    32.             Quaternion toRotation = Quaternion.LookRotation(m_Input, Vector3.up);
    33.  
    34.             transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotateSpeed * Time.deltaTime);
    35.         }
    36.     }
    37.  
    38.     void Update()
    39.     {
    40.         if (IsGrounded() && Input.GetButtonDown("Jump"))
    41.         {
    42.             rb.velocity = Vector3.up * jumpHeight;
    43.         }
    44.  
    45.        
    46.     }
    47.  
    48.     public bool IsGrounded()
    49.     {
    50.         return Physics.Raycast(transform.position, Vector3.down, 1f);
    51.     }
    52. }
    53.  
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    to obtain the Camera's forward you just call
    Code (csharp):
    1. camera.transform.forward
    now depending on your setup that might not be what you want.

    the best approach is to make your logic in local space and ignore all world transformation.
    then when you decide you need that to be affected by the world transformation, you convert your points (or directions) like this
    Code (csharp):
    1. point = transform.TransformPoint(point);
    2. direction = transform.TransformVector(direction);
    TransformPoint
    In your case, you want to use your camera's transform.

    To get back to local space, you use
    InverseTransformPoint
     
    NameyMcNameFace likes this.
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    The difference between TransformPoint and TransformVector is simple, but also drastic.

    This is because Vector2 and Vector3 are used in both contexts, both as a point and as a vector.
    So you must be mindful of the particular context, because in case of a point, any transformation has to take into account the origin as well. This is not true with vectors, especially if they're unit directions, so this makes TransformVector slightly cheaper.

    If you can provide me with a really tiny simple setup, in one script, I might find some time to show you how to change it so that it's affected by camera orientation as well.

    I can't do it otherwise atm. I'm literally moving out from my apt the next day, and the next week will be a hassle and I'll be absent.
     
    NameyMcNameFace likes this.
  4. NameyMcNameFace

    NameyMcNameFace

    Joined:
    Oct 10, 2022
    Posts:
    12

    I attached the super basic scene I've been working on the movement in. If you are able to have a quick look at it I would love to hear your thoughts.

    That being said I appreciate you pointing me in the right direction! I'll take a look at testing both TransformPoint and InverseTransformPoint to see which I think works better for my case!
     

    Attached Files: