Dave
Dave

Reputation: 161

Problems moving camera in unity

My problem is I want to point my camera towards the ground but still move it around horizontally. The problem is when I move the camera forwards, because it has been pointed at an angle towards the ground its local z axis runs through the ground. So when you move the camera forward it follows this axis and descends to the ground.

How would I point the camera towards the ground but maintain horizontal axis?

I have unity version 3.4 it is not pro and I’m coding in C#.

Any help appreciated as I have just started trying to learn unity.

Upvotes: 3

Views: 12959

Answers (1)

Jerdak
Jerdak

Reputation: 4056

I assume you're using Camera.transform.translate? If so, modify your script to do something like:

Vector3 pos = Camera.transform.position;
pos += new Vector3(1,0,1); //Translate 1 unit on x, and 1 unit on z
Camera.transform.position = pos;

For a more complete example, here is my MouseLook() code:

    void MoveCamera(){
    Vector3 oPos = this.transform.position;
    Vector3 newPos = this.transform.position + Translation;
    Vector3 forward = Camera.main.transform.forward;
    Vector3 sideways = Camera.main.transform.right;
    Vector3 up = Camera.main.transform.up;

    newPos = oPos + forward * Translation.z;
    newPos = newPos + sideways * Translation.x;


    if(!_isMouseLook){
        //not mouse look so reset position to original height. 
        //Still apply a Translation as it is tied to the mouse wheel.
        newPos.y = oPos.y + Translation.y;
    } else {
        newPos.y = newPos.y + Translation.y;
    }
    //Clamp height between terrain floor + camera offset and some max height C.
    newPos.y = Mathf.Clamp(newPos.y,Terrain.activeTerrain.SampleHeight(oPos),MaxHeight);
    this.transform.position = newPos;

    //Reset translation values
    Translation = new Vector3(0,0,0);
}

That doesn't contain all of my code but I think you get the gist.

Upvotes: 3

Related Questions