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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

make apply force direction change with camera

Discussion in 'Editor & General Support' started by ShaunLK3, Feb 28, 2021.

  1. ShaunLK3

    ShaunLK3

    Joined:
    Jul 27, 2020
    Posts:
    6
    Hello,
    I'm working on making a ball move around in 3d, from a 3rd person view

    I have a Freelook camera assigned and I'm working on creating some camera control to rotate around the player.

    I'm running into the issue where my ApplyForce function for movement is relative to the X and Z vectors of the scene.

    I want what the ApplyForce function *perceives* as X and Y to be from the camera orientation, not from what they are with the scene.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    Steps:

    - put the X/Z values in a Vector3

    - get the angular heading of the camera

    - multiply the X/Z values by the rotation of the camera

    - use the resulting rotated X/Z vector

    The middle two steps would be something like

    Code (csharp):
    1. var WorldXZ = new Vector3(inputX, 0, inputY);
    2.  
    3. var CameraHeading = MyCamera.transform.eulerAngles.y;
    4.  
    5. var LocalXZ = Quaternion.Euler( 0, CameraHeading, 0) * WorldXZ;
    6.  
    7. // now use LocalXZ to move
    If you want to see it in action, check my proximity_buttons project. Look in the
    DemoCameraRotatedControls
    scene.

    proximity_buttons is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/proximity_buttons

    https://github.com/kurtdekker/proximity_buttons

    https://gitlab.com/kurtdekker/proximity_buttons

    https://sourceforge.net/projects/proximity-buttons/
     
  3. ShaunLK3

    ShaunLK3

    Joined:
    Jul 27, 2020
    Posts:
    6
    Thanks so much for the response!
    ....I don't know what a lot of this means!
    I see that this is creating a variable that uses the camera rotation, and that I need to incorporate LocalXZ into my script somewhere, I'm just not sure where exactly.

    public class VelocityMove : MonoBehaviour
    {
    public float force = 1.0f;

    Rigidbody rb;

    private void Start()
    {
    rb = GetComponent<Rigidbody>();

    }


    void FixedUpdate()
    {
    float xMov = Input.GetAxisRaw("Horizontal") * force;
    float zMov = Input.GetAxisRaw("Vertical") * force;



    rb.AddForce(xMov, 0, zMov, ForceMode.Impulse);

    }
    }
     
  4. ShaunLK3

    ShaunLK3

    Joined:
    Jul 27, 2020
    Posts:
    6
    Got it! Thank you!
     
    Kurt-Dekker likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    Awesome! And if you reply with your fully-functional snippet of code below, perhaps someone else in the future can benefit!
     
  6. ShaunLK3

    ShaunLK3

    Joined:
    Jul 27, 2020
    Posts:
    6
    Needs cleaned a little, but is functional
    {
    public float force = 1.0f;

    Rigidbody rb;
    public Camera MainCamera;


    private void Start()
    {
    rb = GetComponent<Rigidbody>();
    MainCamera = Camera.main;
    }





    void FixedUpdate()
    {

    float xMov = Input.GetAxisRaw("Horizontal") * force;
    float zMov = Input.GetAxisRaw("Vertical") * force;

    Vector3 WorldXZ = new Vector3(xMov, 0, zMov);

    var CameraHeading = MainCamera.transform.eulerAngles.y;

    var LocalXZ = Quaternion.Euler(0, CameraHeading, 0) * WorldXZ;

    rb.AddForce(LocalXZ, ForceMode.Impulse);

    }
    }
     
  7. ShaunLK3

    ShaunLK3

    Joined:
    Jul 27, 2020
    Posts:
    6