- Home /
Rotate a camera when my flashlight is on the edge of the screen... Video included
http://www.youtube.com/watch?v=FgHSBbrymkw
As you can see in the video, I have limited my flashlight so that it does not go off-screen. What i want to do with this is when my flashlight reaches the edge, I want the player to rotate as if he was looking. Here are my scripts if you need them: My flashlight Script:
using UnityEngine; using System.Collections;
public class FlashlightTurn : MonoBehaviour {
public float lookSensitivity;
private float yRotation;
private float xRotation;
private float currentYRotation;
private float currentXRotation;
private float yRotationV;
private float xRotationV;
public float lookSmoothDamp;
private Quaternion lookForward;
public float lookSpeed;
public Transform camera;
private Vector3 lookLeftLimit;
void Start () {
lookForward = transform.rotation;
lookLeftLimit = new Vector3(0,130,0);
}
void Update () {
if (!Input.anyKey)
{
yRotation += Input.GetAxis("Mouse X") * lookSensitivity;
xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity;
xRotation = Mathf.Clamp(xRotation, -30, 30);
yRotation = Mathf.Clamp(yRotation, -50, 30);
currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, ref xRotationV, lookSmoothDamp);
currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, ref yRotationV, lookSmoothDamp);
transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0);
}
if(Input.GetKey("w") || Input.GetKey("s") || Input.GetKey("a") || Input.GetKey("d"))
transform.localRotation = Quaternion.Lerp(transform.localRotation, lookForward, lookSpeed * Time.deltaTime);
if (transform.localEulerAngles == lookLeftLimit) {
xRotation += Input.GetAxis("Mouse Y") * lookSensitivity;
transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0);
}
}
}
Answer by ByteSheep · Apr 02, 2013 at 02:41 AM
You should be able to just check if the yRotation variable is smaller or greater than a certain min or max value and depending on that rotate the camera e.g.:
if(yRotation < minRotation)
{
Debug.Log("Turn Left");
cam.transform.Rotate(-Vector3.up * Time.deltaTime * lookSensitivity);
}
else if(yRotation > maxRotation)
{
Debug.Log("Turn Right");
cam.transform.Rotate(Vector3.up * Time.deltaTime * lookSensitivity);
}
I have added a debug line to the code. It should say turn left / right when you rotate the flashlight.
The camera may not be rotating because you haven't got a proper reference to it.
Also make sure you paste the code on line 37 in your script.
Hmmm... it does say "rotate left/right" but there is no rotation still. When i rotate the flashlight to the left, the rotation says (0, 310, 0) and when its on the right, it says (0, 30, 0). Im not sure if its the local rotation or not
Well what you could do is add a reference to the camera object through a variable:
public Transform cam;
or alternatevely you could use Chronos-L's approach and rather than getting the values of the rotation of the flashlight, get the position of the mouse on the screen.
However if you want to test it using the code above all you'd have to do is:
Add the line of code to the variable section of your code
Change "Camera.main" in my answer to "cam"
And drag the camera to the cam variable in the inspector
Well glad it worked in the end, updated my answer.
Yeah I totally overlooked that the rotation would only be 1 degree per second..
Answer by Chronos-L · Apr 02, 2013 at 02:52 AM
You can use the function I wrote in this question: http://answers.unity3d.com/questions/425712/how-can-i-move-the-camera-when-the-mouse-reaches-t.html
The function will return a Vector2, the x denotes how close the mouse is to the left-right edge while the y denotes the top-bottom edge.
You can do something like this:
//50 is the margin
//The larger the value, the closer it is to the edge
Vector edge = MouseScreenEdge( 50 );
if( edge.x < 0 ) { //Left
//Rotate the camera here
//You can use the edge.x to control how far
//you want to rotate the camera
}
else if( edge.x > 0 ){ //Right
}
Thanks so much for your quick response but I did not want to add another function into my script because I had a lot. Dont want to slow down the game but this response it very good as well. Thanks!