- Home /
make camera move straight
so i made a camera constantly move forward, sort of like a endless runner but i have the camera at an angle looking at the player but with the script that im using, it is moving at the rotation that i have the camera set
using UnityEngine;
public class Camera : MonoBehaviour {
public float forwardSpeed;
void Start () {
}
void Update ()
{
transform.Translate(0f,0f,forwardSpeed*Time.deltaTime);
}
}
Answer by Stratosome · Aug 15, 2018 at 08:25 AM
Hi there!
So, your problem is that because the camera is at an angle, it is moving its local forward instead of, like, on the z axis or something for example, right?
Well, quick solution I think. When you call the transform.Translate function, there is an optional parameter at the end where you can define the 'Space' that the translation occurs in. By default, it is local space, but you can make it world space like this:
transform.Translate(0f, 0f, forwardSpeed * Time.deltaTime, Space.World);
If that doesn't quite do what ya want, let me know!
Answer by Legend_Bacon · Aug 15, 2018 at 12:45 PM
Hello there,
I think Stratosome's answer would work, but alternatively you could also use this solution:
• Make an empty GameObject and call it something like "Camera Parent".
• Set your camera as a child of that GameObject and set its position to 0,0,0.
• Move your movement code from the camera to that empty object.
• Now your empty object moves linearly, regardless of the camera's rotation.
I hope that helps!
Cheers,
~LegendBacon
i tried this as well and worked too thank you very much :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a bubble level (not a game but work tool) 1 Answer
Issue with my camera controls 0 Answers
An OS design issue: File types associated with their appropriate programs 1 Answer