hi again,
You’ve got here the whole code except the variables declarations.
the part with “if (!Input.GetMouseButton(0))” is used to finish the rotation with a nice deccelaration when you released the mouse button
in my gui script, when I click on the home button it turns on the home bool then off if the button isn’t clicked.
So I don’t know if someone has another idea to do that ?
I need to keep the"if (!Input.GetMouseButton(0))" part.
I tried without that part and it doesn’t change anything.
So basically what I need to do:
My rotateCam script makes my camera rotates around a target, with a nice lerp to have acceleration and decceleration.
When you start playing with in and stop to any position, if you click on the home button I need to go back to the starting position exactly like if I restart the “game”.
here is the whole complete code again:
using UnityEngine;
using System.Collections;
public class RotateCam : MonoBehaviour
{
//--------------------------------------------------
// Script By Baghdikian Boris b.baghdikian@gmail.com
//--------------------------------------------------
//-------------------------------Déclaration des Variables-----------------------------
//-------------Rotation Variables---------------------
//Rotation Speed
public float xSpeed = 200.0f;
public float ySpeed = 200.0f;
public float SpeedCoef = 0.02f;
// Y Axis Limits
public float yMinLimit = -80.0f;
public float yMaxLimit = 80.0f;
//Damping
public float RotDamp = 5.0f;
//AmtToRotate in degrees
public float xDeg = 0.0f;
private float yDeg = 0.0f;
//Rotation from current to desired
private Quaternion currentRotation;
public Quaternion desiredRotation;
public Quaternion rotation;
//---------Position Variables--------------------------
// Target
public Transform target;
public Vector3 targetOffset;
// Distances and positions
public float distance = 5.0f;
public float maxDistance = 20.0f;
public float minDistance = 0.6f;
private float currentDistance;
public static float desiredDistance;
private Vector3 position;
//Zoom parameters
public int zoomRate = 150;
public int zoomRateRMB = 550;
public float zoomDamp = 5.0f;
//interrupteur GUI
public static bool interrupteur = true;
//Sensibility Zooms
public int RClicSensibility;
public int MidSensibility;
public Transform thisTransform;
//public Vector3 startPos;
//public Vector3 endPos;
public float time;
public float i;
public float rate;
public static bool home;
//------------------------------- START -------------------------------
// Use this for initialization
void Start()
{
//-------------Set initial Rotation--------------------
rotation = transform.rotation;
currentRotation = transform.rotation;
desiredRotation = transform.rotation;
//desiredRotation = Quaternion.Euler(37, 142, 0);
xDeg = Vector3.Angle(Vector3.right, transform.right);
yDeg = Vector3.Angle(Vector3.up, transform.up);
//-----------Set initial Position----------------------
distance = Vector3.Distance(transform.position, target.position);
currentDistance = distance;
desiredDistance = distance;
position = transform.position;
}
//------------------------------- UPDATE -------------------------------
// Update is called once per frame
void Update()
{
if (interrupteur == true)
{
//-------Home-------
if (home == true)
{
desiredRotation = Quaternion.Euler(37, 142, 0);
xDeg = Vector3.Angle(Vector3.right, transform.right);
yDeg = Vector3.Angle(Vector3.up, transform.up);
transform.rotation = Quaternion.Euler(37, 142, 0);
//rotation = Quaternion.Euler(37, 142, 0);
print("up");
}
//--------------Rotation--------------------
//Check if the mouse button is on
if (Input.GetMouseButton(0))
{
//Set the AmtToRotate
xDeg += Input.GetAxis("Mouse X") * xSpeed * SpeedCoef;
yDeg -= Input.GetAxis("Mouse Y") * ySpeed * SpeedCoef;
// Make sure that the yDeg is still in the Y limits
yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
//Use the AmtToRotate to rotate the camera
desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
currentRotation = transform.rotation;
//Add a damping
rotation = Quaternion.Slerp(currentRotation, desiredRotation, Time.deltaTime * RotDamp);
transform.rotation = rotation;
}
if (!Input.GetMouseButton(0))
{
currentRotation = transform.rotation;
rotation = Quaternion.Slerp(currentRotation, desiredRotation, Time.deltaTime * RotDamp);
transform.rotation = rotation;
}
//---------------Position------------------
//Set the AmtToMove
desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance) * MidSensibility;
if (Input.GetMouseButton(1))
{
desiredDistance -= Input.GetAxis("Mouse Y") * RClicSensibility;
}
//Clamp the zoom min/max
desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
//Add the damping
currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDamp);
// New position
position = target.position - (rotation * Vector3.forward * currentDistance + targetOffset);
transform.position = position;
}
}
private static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}