Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

[SOLVED]Moving object in the direction of camera view

Discussion in 'Scripting' started by end0re, Sep 14, 2009.

  1. end0re

    end0re

    Joined:
    Sep 14, 2009
    Posts:
    2
    Well basically I would like to move an object which has attached camera - it can rotate, but how do I make it move in the direction where the object camera points to?
    I just started learning Unity :oops:
     
  2. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,572
    A quick technique is to do this:

    transform.position = transform.position + Camera.main.transform.forward * distance * Time.deltaTime;

    Basically, it says move the object this script is on in the direction the camera is facing distance units per second. You'll have to declare a global variable for distance like:

    var distance : float = 5;

    though. Time.deltaTime is the variable that makes sure movements are done per second if done inside function Update() or once per frame.
     
    KSZLAGK, CandraWi, MHF7517 and 9 others like this.
  3. end0re

    end0re

    Joined:
    Sep 14, 2009
    Posts:
    2
    Omg - big, big thanky!

    All works now! :p
     
  4. g-hoot

    g-hoot

    Joined:
    Apr 18, 2015
    Posts:
    69
    I found a lot of complicated answers to this question, and then found this. Thanks Gargerath Sunman!
     
  5. Vitaly-Abilevich

    Vitaly-Abilevich

    Joined:
    Nov 6, 2013
    Posts:
    61
    Thank you for the tip! It's awesome, but I am facing a problem here, my character walks over a planet using an orbital camera that turns arround him, the problem is that if I look from above the player it wolks into the planet, since the gravity is very low, if I walk backwards, while looking from the very top, it flyes awar from the planet
     
  6. rahulpatil6220375

    rahulpatil6220375

    Joined:
    Dec 10, 2018
    Posts:
    19
    camera rotate up and down move togrther in unity
     
  7. donfour

    donfour

    Joined:
    Aug 12, 2018
    Posts:
    5
    Hi, may I ask how can I do the same with rigidbody? To make matters more complicated, my camera is also able to tilt downwards / upwards, but I still want my rigidbody to move parallel to the ground. How can I do so?

    I currently am using a joystick to control the velocity of a cube like so:

    Code (CSharp):
    1.  
    2. void Update()
    3.         {
    4.             var rigidbody = GetComponent<Rigidbody>();
    5.             rigidbody.velocity = new Vector3(joystick.Horizontal * 3f,
    6.                                              rigidbody.velocity.y,
    7.                                              joystick.Vertical * 3f);
    8.         }
    9.  
    Thank you so much in advance!
     
    Last edited: Jan 9, 2019
  8. Lucvd123

    Lucvd123

    Joined:
    Sep 9, 2018
    Posts:
    2
    A question about this is this line of code 'transform.position = transform.position + Camera.main.transform.forward * distance * Time.deltaTime;' possible to be made with adding in forces
     
    loldude482 likes this.
  9. ewendel88

    ewendel88

    Joined:
    Feb 14, 2019
    Posts:
    3
    How can I move at any angle to the camera, not just forward?
     
  10. jupitr

    jupitr

    Joined:
    Mar 20, 2020
    Posts:
    1
    This did not work at all. I am trying to make a 3d fps and my character started moving on its own. It went in the opposite direction as the camera and sunk into the ground.
     
  11. sumpfkraut

    sumpfkraut

    Joined:
    Jan 18, 2013
    Posts:
    157
    for any direction you can combine forward and right
    Code (CSharp):
    1. private float _dragObjectSpeed = 0.2f;
    2. transform.position = transform.position + Camera.main.transform.forward * Input.GetAxis("Mouse Y") * _dragObjectSpeed + Camera.main.transform.right * Input.GetAxis("Mouse X") * _dragObjectSpeed;
     
  12. ansonisawesome24

    ansonisawesome24

    Joined:
    Dec 23, 2019
    Posts:
    13
    when my player looks up, the player jumps, how do i fix that?
     
unityunity