- Home /
Getting 3rd person to look in direction of camera when right mouse clicked
I am trying to make it so that when I click the right mouse button (without dragging) and release, the player imediately rotates towards the camera direction (only y rotation). I currently have it set so that the camera rotates towards the chracter direction when t he right mouse button is clicked (the opposite of what I want). It works perfectly right now, but to do what I want, I think I will need to implement my 3rd person controller script (I used the standard asset one), but I was wondering the following:
- How to tell the 3rd person script to execute a function from the MouseLook script
- How to rotate the 3rd person to the camera's Y rotation
Here is my MouseLook script (I wont post the 3rd person controller, it is way too long, and built in the standard assets, so you can go there to see it):
var sensitivityX : float = 10.0; var sensitivityY : float = 10.0;
var minimumX : float = -360.0; var maximumX : float = 360.0;
var minimumY : float = -60.0; var maximumY : float = 25.0;
var minimumScroll : float = 16.0; var maximumScroll : float = 20.0;
private var rotationY : float = 0.0; private var rotationX : float = 0.0; private var mouseScroll : float = 18.0; private var mouseDrag : boolean = false; private var mouseX : float; private var mouseY : float; private var mouseStartVector; private var mouseDownStart : boolean = false; private var mouseWasDown : boolean = false; private var haveOverriden : boolean = false;
function Update () { mouseX += Input.GetAxis("Mouse X"); mouseY += Input.GetAxis("Mouse Y"); if (Input.GetButton ("Fire2")) { if (mouseDownStart == false){ mouseStartVector = Vector2(mouseX,mouseY); mouseDownStart = true; } haveOverriden = true; rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") sensitivityX; rotationY += Input.GetAxis("Mouse Y") sensitivityY; rotationY = Mathf.Clamp (rotationY, minimumY, maximumY); mouseWasDown = true; if (mouseX == mouseStartVector.x && mouseY == mouseStartVector.y){ mouseDrag = false; } else{ mouseDrag = true; } } else{ if (mouseWasDown && mouseDrag == false){ transform.localEulerAngles = Vector3(0,0,0); rotationX = 0.0; rotationY = 0.0; haveOverriden = false; } mouseWasDown = false; mouseDownStart = false; mouseDrag = false; } if (haveOverriden){ transform.localEulerAngles = Vector3(-rotationY, rotationX, 0); } mouseScroll -= Input.GetAxis ("Mouse ScrollWheel"); mouseScroll = Mathf.Clamp (mouseScroll, minimumScroll, maximumScroll); var cameraZoom = Camera.main; cameraZoom.fieldOfView = mouseScroll*20+50; }
Answer by pigi5 · Feb 08, 2011 at 11:08 PM
Nevermind, I figured it out. The built in third preson script is really bad and clunky to be honest. I found another one and edited it to my liking. I just set a static variable on that script the the mouseLook script can change to reset the guy's rotation.
please share where you found a better one! the one that ships with Unity is just a bad port from the Lerpz tutorial
Your answer
Follow this Question
Related Questions
Relative Movement Problem 1 Answer
Turning character with transform.Rotate 0 Answers
Rotating a camera that has MouseLook.cs attached 0 Answers
Character Face Camera Direction 1 Answer