- Home /
How to move object through the view direction of the mainCamera
First, I must say that I've searched in the forum and google, but it seems that noone have met this problem, and sorry everyone for my bad English
In the forum, everyone said that " just use Camera.main.transform.forward, which indicates the forward direction of the camera as a vector".
So i wrote
if (Input.GetKey(KeyCode.UpArrow))
{
this.transform.position = Camera.mainCamera.transform.position - Camera.main.transform.forward;
}
if (Input.GetKey(KeyCode.DownArrow))
{
this.transform.position = Camera.mainCamera.transform.position + Camera.main.transform.forward;
}
Comment
Answer by robertbu · May 27, 2013 at 04:41 AM
Try this:
#pragma strict
var speed = 4.0; // Units per second
function Update () {
if (Input.GetKey(KeyCode.UpArrow))
{
transform.position -= Camera.main.transform.forward * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.DownArrow))
{
transform.position += Camera.main.transform.forward * speed * Time.deltaTime;
}
}
Your answer