Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mouse Controlled Camera third-person #39

Open
gamingdevotee opened this issue Nov 6, 2016 · 1 comment
Open

Mouse Controlled Camera third-person #39

gamingdevotee opened this issue Nov 6, 2016 · 1 comment
Assignees

Comments

@gamingdevotee
Copy link
Contributor

I want to make a camera that can move around behind the player in third-person, it should be able to rotate using mouse-based movement on the X-axis and will make it easier to track the player.

@notleeandrew notleeandrew self-assigned this Nov 7, 2016
@notleeandrew
Copy link
Contributor

To use this you need to tag the player as "Player", the rest is already built into unity. You then need to attach it to the camera.

public GameObject target;
public float rotateSpeed = 5f;
Vector3 offset;
// Use this for initialization
void Start () {
    offset = target.transform.position - transform.position;
    target = GameObject.FindGameObjectWithTag ("Player");
}

// Update is called once per frame
void LateUpdate () {
    float horizontal = Input.GetAxis ("Mouse X") * rotateSpeed;
    target.transform.Rotate(0, horizontal, 0);

    float desiredAngle = target.transform.eulerAngles.y;
    Quaternion rotation = Quaternion.Euler(0, desiredAngle, 0);
    transform.position = target.transform.position - (rotation * offset);

    transform.LookAt(target.transform);
}

}

@Ksosa Ksosa self-assigned this Nov 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants