Can someone tell me how to set minimum and maximum rotation to the TouchLook script?
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Touch Look")]
public class TouchLook : MonoBehaviour {
public float sensitivityX = 5.0f;
public float sensitivityY = 5.0f;
public bool invertX = false;
public bool invertY = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.touches.Length > 0)
{
if (Input.touches[0].phase == TouchPhase.Moved)
{
Vector2 delta = Input.touches[0].deltaPosition;
float rotationZ = delta.x * sensitivityX * Time.deltaTime;
rotationZ = invertX ? rotationZ : rotationZ * -1;
float rotationX = delta.y * sensitivityY * Time.deltaTime;
rotationX = invertY ? rotationX : rotationX * -1;
transform.localEulerAngles += new Vector3(rotationX, rotationZ, 0);
}
}
}
}
rotationX = Mathf.Clamp(invertY ? rotationX : rotationX * -1, MinAngle, MaxAngle);
Thank you. I have it now like this but it does not work.
Can someone tell me what is wrong?
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Touch Look")]
public class TouchLook : MonoBehaviour {
public float sensitivityX = 5.0f;
public float sensitivityY = 5.0f;
public float MinAngleX = -60F;
public float MaxAngleX = 60F;
public float MinAngleZ = -60F;
public float MaxAngleZ = 60F;
public bool invertX = false;
public bool invertY = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.touches.Length > 0)
{
if (Input.touches[0].phase == TouchPhase.Moved)
{
Vector2 delta = Input.touches[0].deltaPosition;
float rotationZ = delta.x * sensitivityX * Time.deltaTime;
// rotationZ = invertX ? rotationZ : rotationZ * -1;
rotationZ = Mathf.Clamp(invertX ? rotationZ : rotationZ * -1, MinAngleZ, MaxAngleZ);
float rotationX = delta.y * sensitivityY * Time.deltaTime;
// rotationX = invertY ? rotationX : rotationX * -1;
rotationX = Mathf.Clamp(invertY ? rotationX : rotationX * -1, MinAngleX, MaxAngleX);
transform.localEulerAngles += new Vector3(rotationX, rotationZ, 0);
}
}
}
}
Ah, u used += in the line 38, i didn’t notice that. You need to clamp the result, not the addendum
var localEuler = transform.localEulerAngles;
localEuler.z = Mathf.Clamp(localEuler.z +( invertX ? rotationZ : rotationZ * -1), MinAngleZ, MaxAngleZ);
// same for x
transform.localEulerAngles = localEuler;
something like this
Doing it that way will result in Euler-angles-related strangeness and glitchy rotation. Highly recommended further reading. Never use the Euler angles from a transform as your input.
You want to track your X and Y rotations on your own, add the delta to those values, then set the rotation to them (as palex says, don’t use += there).
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Touch Look")]
public class TouchLook : MonoBehaviour {
public float sensitivityX = 5.0f;
public float sensitivityY = 5.0f;
public bool invertX = false;
public bool invertY = false;
float rotationZ = 0f;
float rotationX = 0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.touches.Length > 0)
{
if (Input.touches[0].phase == TouchPhase.Moved)
{
Vector2 delta = Input.touches[0].deltaPosition;
rotationZ += (invertX ? 1f : -1f) * delta.x * sensitivityX * Time.deltaTime;
rotationX += (invertY ? 1f : -1f) * delta.y * sensitivityY * Time.deltaTime;
transform.localEulerAngles = new Vector3(rotationX, rotationZ, 0);
}
}
}
}
1 Like
Thanks for your help!
I have it now like this and it seems to work like I want:
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Touch Look")]
public class TouchLook : MonoBehaviour {
public float sensitivityX = 5.0f;
public float sensitivityY = 5.0f;
public bool invertX = false;
public bool invertY = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.touches.Length > 0)
{
if (Input.touches[0].phase == TouchPhase.Moved)
{
Vector2 delta = Input.touches[0].deltaPosition;
float rotationZ = delta.x * sensitivityX * Time.deltaTime;
rotationZ = invertX ? rotationZ : rotationZ * -1;
float rotationX = delta.y * sensitivityY * Time.deltaTime;
rotationX = invertY ? rotationX : rotationX * -1;
transform.localEulerAngles += new Vector3(rotationX, rotationZ, 0);
}
}
// Limit rotation
var localEuler = transform.localEulerAngles;
if (localEuler.x > 180)
localEuler.x = -360 + localEuler.x;
localEuler.x = Mathf.Clamp(localEuler.x, -10, 45);
if (localEuler.y > 180)
localEuler.y = -360 + localEuler.y;
localEuler.y = Mathf.Clamp(localEuler.y, -30, 30);
transform.localEulerAngles = localEuler;
}
}
Please read the article I posted - your new code is likely to produce seemingly-random rotation glitches.
Thanks for your info! I have just tested it. Attached the script on a camera and its running in a first person shooter on my Android device without any problems.