Hi All!
I have a problem with a project: a cube in which a camera moves around in the three axis with the mouse.
The XY axes are controlled by mouse X and mouse Y, and the zoom (not FOV) with the Mouse ScrollWheel.
There is the script that moves the camera:
using UnityEngine;
using System.Collections;
public class CameraMotion2 : MonoBehaviour {
public float zoomSpeed = 4.0f;
public float rotationSpeed = 0.5f;
float lastMousePos;
void Update () {
// Wheel Zoom
transform.Translate (Vector3.forward * Input.GetAxis ("Mouse ScrollWheel") * zoomSpeed);
// Camera X Rotation
Vector2 xMouseEdge = MouseScreenEdge ((int)(Screen.width * 0.3f));
if(!(Mathf.Approximately (xMouseEdge.x, 0.0f))) {
//Move your camera depending on the sign of mouse.Edge.x
if (xMouseEdge.x > 0) {
//Move Left
transform.Rotate (new Vector3 (0, xMouseEdge.x, 0) * Time.deltaTime * rotationSpeed);
}
else if (xMouseEdge.x < 0) {
//Move right
transform.Rotate (new Vector3 (0, xMouseEdge.x, 0) * Time.deltaTime * rotationSpeed);
}
}
// Camera Y Rotation
Vector2 yMouseEdge = MouseScreenEdge ((int)(Screen.height * 0.3f));
if (!(Mathf.Approximately (yMouseEdge.y, 0.0f))) {
if (yMouseEdge.y > 0 && transform.rotation.x > -0.5f) {
transform.Rotate (new Vector3 (-yMouseEdge.y, 0, 0) * Time.deltaTime * rotationSpeed);
}
else if (yMouseEdge.y < 0 && transform.rotation.x < 0.5f) {
transform.Rotate (new Vector3 (-yMouseEdge.y, 0, 0) * Time.deltaTime * rotationSpeed);
}
}
}
void OnCollisionEnter () {
StartCoroutine (ResetPos (0.5f));
}
Vector2 MouseScreenEdge (int margin) {
//Margin is calculated in px from the edge of the screen
Vector2 half = new Vector2 (Screen.width * 0.5f, Screen.height * 0.5f);
//If mouse is dead center, (x,y) would be (0,0)
float x = Input.mousePosition.x - half.x;
float y = Input.mousePosition.y - half.y;
//If x is not within the edge margin, then x is 0;
//In another word, not close to the edge
if(Mathf.Abs (x) > half.x - margin) {
x += (half.x - margin) * ((x < 0) ? 1 : -1);
}
else {
x = 0.0f;
}
if(Mathf.Abs(y) > half.y - margin) {
y += (half.y - margin) * ((y < 0) ? 1 : -1);
}
else {
y = 0.0f;
}
return new Vector2(x, y);
}
IEnumerator ResetPos (float time) {
float elapsedTime = 0.0f;
Vector3 targetPos = new Vector3 (transform.position.x, transform.position.y, transform.position.z - 1.0f);
while (elapsedTime < time) {
elapsedTime += Time.deltaTime;
transform.position = Vector3.Lerp (transform.position, targetPos, (elapsedTime / time));
}
yield return null;
}
IEnumerator ResetRot (float time) {
float elapsedTime = 0.0f;
Quaternion targetRotation = Quaternion.Euler (new Vector3 (0, transform.rotation.y, transform.rotation.z));
while (elapsedTime < time) {
elapsedTime += Time.deltaTime;
transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation, (elapsedTime / time));
}
yield return null;
}
}
But that have some problems…
First of all at line 14: the wheel translation is no smooth (i need a sort of ease out)
Second: The X rotation is activated by the mouse position Y the more it reaches the top or bottom borders of the screen, but I need that the X rotation returns back when the mouse Y returns near the center of the screen.
Third: the two coroutines don’t work (the animation is istance and not in the whole time setted).
I know it’s not a simple script, but i really need help.
PS The MouseScreenEdge is a function that calculates the amount of space from the borders of the screen.