[SOLVED]Moving object in the direction of camera view

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 :sweat_smile:

2 Likes

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.

33 Likes

Omg - big, big thanky!

All works now! :stuck_out_tongue:

2 Likes

I found a lot of complicated answers to this question, and then found this. Thanks Gargerath Sunman!

1 Like

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

camera rotate up and down move togrther in unity

1 Like

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:

void Update()
        {
            var rigidbody = GetComponent<Rigidbody>();
            rigidbody.velocity = new Vector3(joystick.Horizontal * 3f,
                                             rigidbody.velocity.y,
                                             joystick.Vertical * 3f);
        }

Thank you so much in advance!

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

1 Like

How can I move at any angle to the camera, not just forward?

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.

1 Like

for any direction you can combine forward and right

private float _dragObjectSpeed = 0.2f;
transform.position = transform.position + Camera.main.transform.forward * Input.GetAxis("Mouse Y") * _dragObjectSpeed + Camera.main.transform.right * Input.GetAxis("Mouse X") * _dragObjectSpeed;
1 Like

when my player looks up, the player jumps, how do i fix that?

change the value of the Y scale on the movement to 0.0f
6531698--737804--upload_2020-11-17_6-53-56.png

6531698--737804--upload_2020-11-17_6-53-56.png

jupitr just put the line under the if statement that specifies what happens when you press w

but why we use distance?

I am building a 360 photo sphere. When the Up Arrow is pressed, I want the camera to move from the center of the sphere closer to the edge and stop at a set distance. Then when the Up Arrow is released, I want the camera to return to the center of the sphere.
I am having trouble adapting this script to meet that need.

I have this script attached to the camera. It works when I press up (although very slow). how can i make it return to the origin position when I let go of the up arrow?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TrackPosition : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.UpArrow))
        {
            transform.position += Camera.main.transform.forward * Time.deltaTime;
        }
    }
}

use an else if statement. that should work. as for which else if statement you need to use? I don’t know. someone else on this tread might know though

Hi! Can you please tell me the solution for this, I am facing the same problem.

soo… i cant seem to find anything, so can anyone help with my code? its supposed to move with the camera direction, but it takes the same “Forward” every time.

my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class move : MonoBehaviour
{
public float speed = 1000f;
public Rigidbody rb;
private bool Grounded;
public float jump;

void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == “Ground”)
{
Grounded = true;
}
}
void Update()
{
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
transform.Rotate(0.0f, 2.0f, 0.0f);
}
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
transform.Rotate(0.0f, -2.0f, 0.0f);
}
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
{
rb.AddForce(Vector3.forward * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
{
rb.AddForce(Vector3.back * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.Space) && Grounded == true)
{
rb.AddForce(Vector2.up * jump * Time.deltaTime);
Grounded = false;
}

}
}

thanks!

use back and left and righ