- Home /
3rd Person Camera inversion
So basically I used the tutorial on https://code.tutsplus.com/tutorials/unity3d-third-person-cameras--mobile-11230 to make my 3rd person camera and it works great but I want the camera to be inverted when I move the mouse up the camera looks down and vice versa. Anyone able to help?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraScript : MonoBehaviour {
//It's the ship
public GameObject Ship;
public float TurnSpeed;
//How far away the camera should be from the ship
private Vector3 offset;
void Start()
{
//May act funny when not maximaized in editor
//Hides mouse
Cursor.visible = false;
offset = Ship.transform.position - transform.position;
}
private void LateUpdate()
{
float horizontal = Input.GetAxis("Mouse X") * TurnSpeed;
Ship.transform.Rotate(0, horizontal, 0);
float desiredAngle = Ship.transform.eulerAngles.y;
Quaternion rotation = Quaternion.Euler(0, desiredAngle, 0);
//Take the distance between the camera and the ship
transform.position = Ship.transform.position - (rotation * offset);
//Lookat the Ship
transform.LookAt(Ship.transform);
}
}
Answer by Chris_Payne_QS · Apr 28, 2017 at 09:00 AM
Your question implies that the camera already moves in the vertical axis and you want to invert it, but there's no code here to do that - if so it may be handled by a different script?
But if you're really asking how to ADD that functionality...you basically want to do the same thing with the vertical axis:
float horizontal = Input.GetAxis("Mouse X") * TurnSpeed;
float vertical = Input.GetAxis("Mouse Y") * PitchSpeed;
Ship.transform.Rotate(vertical, horizontal, 0);
Wouldn't this just swap the X-axis with the Y-axis? I'm pretty sure he just wants to make up down and down up like selecting invert in a shooter game? all you have to do is multiply Y input by -1 say if a bool invert is checked.
Also why are you rotating the transform of the "ship"? Am I missing something?
Thank you, i had something on another script similar to this but I removed it thinking it was redundant after replacing it and adding the negative sign it worked perfectly. :)
Answer by AtGfx · Apr 28, 2017 at 08:24 AM
What about simply take the opposite of your x position ?
Ship.transform.Rotate(0, -horizontal, 0);