- Home /
Unity input to rotate camera
I Have this code, and input to rotate works in WebGL on browser, but not working when I opening the same from Mobile browser. Could you please help me.
using UnityEngine;
public class CameraController : MonoBehaviour { public GameObject heads; Camera cam; public bool isControlable; private Vector3 screenPoint; private Vector3 offset; public GameObject target; public float distance = 1200f; public float xSpeed = 2f; public float ySpeed = 2f;
public float yMinLimit = -80f;
public float yMaxLimit = 80f;
public float yMinLimit1 = -60f;
public float yMaxLimit1 = 60f;
public float distanceMin = 600f;
public float distanceMax = 1200f;
public float smoothTime = 2f;
public float rotationYAxis = 0.0f;
float rotationXAxis = 0.0f;
float velocityX = 0.0f;
float velocityY = 0.0f;
float moveDirection = -1;
void Start()
{
cam = GetComponentInChildren<Camera>();
Vector3 angles = transform.eulerAngles;
rotationYAxis = (rotationYAxis == 0) ? angles.y : rotationYAxis;
rotationXAxis = angles.x;
Rigidbody rigidbody = GetComponent<Rigidbody>();
// Make the rigid body not change rotation
if (rigidbody)
{
rigidbody.freezeRotation = true;
}
}
void LateUpdate()
{
if (target)
{
if (Input.GetMouseButton(0) && isControlable)
{
velocityX = xSpeed * Input.GetAxis("Mouse X");
velocityY = ySpeed * Input.GetAxis("Mouse Y");
}
if (isControlable)
{
if (heads.activeSelf)
{
distance = distanceMax;
target.transform.position = new Vector3(-0.5444031f, -176f, -11f);
rotationYAxis += velocityX;
rotationXAxis -= velocityY;
rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
Quaternion rotation = toRotation;
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.transform.position;
transform.rotation = rotation;
transform.position = position;
velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
screenPoint = cam.WorldToScreenPoint(target.transform.position);
offset = target.transform.position - cam.ScreenToWorldPoint(new Vector3(moveDirection * Input.mousePosition.x, moveDirection * Input.mousePosition.y, screenPoint.z));
}
if (!heads.activeSelf)
{
distance = distanceMin;
target.transform.position = new Vector3(-0.5444031f, 308f, -11f);
rotationYAxis += velocityX;
rotationXAxis -= velocityY;
rotationXAxis = ClampAngle(rotationXAxis, yMinLimit1, yMaxLimit1);
Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
Quaternion rotation = toRotation;
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.transform.position;
transform.rotation = rotation;
transform.position = position;
velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
screenPoint = cam.WorldToScreenPoint(target.transform.position);
offset = target.transform.position - cam.ScreenToWorldPoint(new Vector3(moveDirection * Input.mousePosition.x, moveDirection * Input.mousePosition.y, screenPoint.z));
}
}
}
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}
Comment