How to clamp camera in Unity3D - c#

My code is not working, I am trying to clamp the camera, but its not working. How to clamp the camera?
using UnityEngine;
using System.Collections;
public class MoveCamera : MonoBehaviour
{
public float sensitivity = 4.0f;
private Vector3 mouseOrigin;
private bool isRotating;
private float speed = 2.0f;
private float minX = -45.0f;
private float maxX = 45.0f;
private float minY = -10.0f;
private float maxY = 10.0f;
float rotationY = 0.0f;
float rotationX = 0.0f;
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
mouseOrigin = Input.mousePosition;
isRotating = true;
}
if (!Input.GetMouseButton (0))
isRotating = false;
if (isRotating) {
Vector3 pos = Camera.main.ScreenToViewportPoint (Input.mousePosition - mouseOrigin);
transform.RotateAround (transform.position, transform.right, -pos.y * sensitivity);
transform.RotateAround (transform.position, Vector3.up, pos.x * sensitivity);
rotationY = Mathf.Clamp (rotationY, minY, maxY);
rotationX = Mathf.Clamp (rotationX, minX, maxX);
transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
}
}
}

You forgot to get the value of rotationX and rotationY from your transform after rotating it. Try this :
if (isRotating) {
Vector3 pos = Camera.main.ScreenToViewportPoint (Input.mousePosition - mouseOrigin);
transform.RotateAround (transform.position, transform.right, -pos.y * sensitivity);
transform.RotateAround (transform.position, Vector3.up, pos.x * sensitivity);
rotationY = Mathf.Clamp (transform.localEulerAngles.y, minY, maxY);
rotationX = Mathf.Clamp (transform.localEulerAngles.x, minX, maxX);
transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
}

Here's the code of Mathf.Clamppublic static float Clamp(float value, float min, float max) {
if (value < min) {
value = min;
} else if (value > max) {
value = max;
}
return value;
}
Use an IL reverse tool such as (EXTERNAL LINK)ILSPY if you are unsure how a call works in .NET (Unity / Mono / etc).
Based on the code you posted and understanding Mathf.Clamp should work as intended, the issue is most probably laying in your code, at least at one point, e.g. here:
rotationY = Mathf.Clamp (rotationX, minY, maxY); //note it's rotation "X" instead of "Y"
rotationX = Mathf.Clamp (rotationX, minX, maxX);
If this still did not sort out the issue, use Debug.Log to see variable values to find where you make the mistake(s).
If you can't sort out like this, you'll have a clear picture what exactly you can't do and can post a much cleaner question and expect a clean answer.
Hope this helps!

OK, So I fixed it. Here is complete code.
using UnityEngine;
using System.Collections;
public class MoveCamera : MonoBehaviour
{
public float sensitivity = 4.0f;
private Vector3 mouseOrigin;
private bool isRotating;
public GameObject cam;
void Start()
{
}
protected float ClampAngle(float angle, float min, float max) {
angle = NormalizeAngle(angle);
if (angle > 180) {
angle -= 360;
} else if (angle < -180) {
angle += 360;
}
min = NormalizeAngle(min);
if (min > 180) {
min -= 360;
} else if (min < -180) {
min += 360;
}
max = NormalizeAngle(max);
if (max > 180) {
max -= 360;
} else if (max < -180) {
max += 360;
}
return Mathf.Clamp(angle, min, max);
}
protected float NormalizeAngle(float angle) {
while (angle > 360)
angle -= 360;
while (angle < 0)
angle += 360;
return angle;
}
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
mouseOrigin = Input.mousePosition;
isRotating = true;
}
if (!Input.GetMouseButton (0))
isRotating = false;
if (isRotating) {
cam.transform.localEulerAngles = new Vector3(0, ClampAngle(cam.transform.localEulerAngles.y, -45, 45), 0);
Vector3 pos = Camera.main.ScreenToViewportPoint (Input.mousePosition - mouseOrigin);
transform.RotateAround (transform.position, transform.right, -pos.y * sensitivity);
transform.RotateAround (transform.position, Vector3.up, pos.x * sensitivity);
}
}
}

Related

Mathf.Clamp is messing with my camera movement script

i am making a game that uses the WASD keys to move the camera between different objects and am using Mathf.Clamp to limit it but it now makes it so the movement doesn't work and i only move diagonal which shouldn't be possible without multiple key presses.
{
public Vector3 startPos;
public float moveDistance = 1;
public float minX;
public float minZ;
public float maxX;
public float maxZ;
void Start()
{
transform.position = startPos;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
transform.position += new Vector3(0, 0, moveDistance);
}
else if (Input.GetKeyDown(KeyCode.S))
{
transform.position += new Vector3(0, 0, -moveDistance);
}
else if (Input.GetKeyDown(KeyCode.D))
{
transform.position += new Vector3(moveDistance, 0, 0);
}
else if (Input.GetKeyDown(KeyCode.A))
{
transform.position += new Vector3(-moveDistance, 0, 0);
}
transform.position = new Vector3(Mathf.Clamp(transform.position.x, minX, maxX), transform.position.y, Mathf.Clamp(transform.position.x, minZ, maxZ));
}
}
looks like Ruzihm already answered your question.
I recently ran into some problems with Mathf.Clamp with negative numbers.
So I wanted to share the solution.
Cannot remember where I found it so sorry I can't credit the person who made it.
public float ClampAngle(float angle, float minAngle, float maxAngle)
{
if (angle < 90 || angle > 270)
{ // if angle in the critic region...
if (angle > 180) angle -= 360; // convert all angles to -180..+180
if (maxAngle > 180) maxAngle -= 360;
if (minAngle > 180) minAngle -= 360;
}
angle = Mathf.Clamp(angle, minAngle, maxAngle);
if (angle < 0) angle += 360; // if angle negative, convert to 0..360
return angle;
}

Change Object's Scale Smoothly with Swipe

I wrote the code below to change an object's scale (X axis) with mouse swipe, it works but it's not smooth, how can I smooth it?
Script:
Vector3 newScale;
private float _previousSwipePosition;
private float newPosition;
if (Input.GetMouseButton(0))
{
_previousSwipePosition = Input.mousePosition.x;
if (newPosition != _previousSwipePosition)
{
if (newPosition - _previousSwipePosition < -2)
{
if (transform.localScale.x <= 1.4f)
{
newScale = transform.localScale;
newScale.x += 0.06f;
transform.localScale = newScale;
}
}
else if (newPosition - _previousSwipePosition > 2)
{
if (transform.localScale.x >= 0.2f)
{
newScale = transform.localScale;
newScale.x -= 0.06f;
transform.localScale = newScale;
}
}
}
newPosition = Input.mousePosition.x;
}
You can use Input.GetAxis("Mouse X") to get smoothed scale of how much the mouse has moved in the last frame. Multiply that by a speed parameter.
Get the power of 2 by that product to get how much to change the current scale. Then, change the scale and clamp it:
public float scaleSpeed = 1f;
// ...
// ignore first frame mouse is pressed
if (Input.GetMouseButton(0) && !Input.GetMouseButtonDown(0))
{
float scaleFactor = Mathf.Pow(2f, Input.GetAxis("Mouse X")
* scaleSpeed);
float newX = Mathf.Clamp(transform.localScale.x * scaleFactor, 0.2f, 1.4f);
transform.localScale = new Vector3(
newX,
transform.localScale.y,
transform.localScale.z);
}
Use Time.deltaTime to smooth
Vector3 newScale;
private float _previousSwipePosition;
private float newPosition;
private float speed = 6f;
private void Update()
{
if (Input.GetKey(KeyCode.A))
{
if (transform.localScale.x <= 1.4f)
{
newScale = transform.localScale;
newScale.x += speed * Time.deltaTime;
transform.localScale = newScale;
}
}
if (Input.GetKey(KeyCode.B))
{
if (transform.localScale.x >= 0.2f)
{
newScale = transform.localScale;
newScale.x -= speed * Time.deltaTime;
transform.localScale = newScale;
}
}
}

How to Limit (clamp) Y axis Rotation for transform.rotatearound Unity

I have a camera that I want to rotate around a point (0,0,0) in all directions, but I want to put a clamp on it so that it can't go too far above or below the point. I have seen this question answered for the left and right directions before but never for the vertical one.
I have tried converting the code from these two questions (that basically say the same thing) to work in the vertical direction, but it bugs out at some points along the rotation, and I can't figure out why.
First Question, Second Question
And this is how I tried to convert it:
//how much we want to rotate by this frame
float rotX = Input.GetAxis("Mouse X") * rotSpeed;
float rotY = Input.GetAxis("Mouse Y") * rotSpeed; //(before clamping)
//find current direction
Vector3 currentDirection = transform.position - Vector3.zero;
//find current angle between basis for clamp & where we are now
float angle = Vector3.Angle(Vector3.forward, currentDirection);
//finds out if it's up or down
if (Vector3.Cross(Vector3.forward, currentDirection).x < 0) angle = -angle;
//find out how much you can move without violating limits
float newAngle = Mathf.Clamp(angle + rotY, yMinLimit, yMaxLimit);
//grabs how much you are allowed to move the angle from the current angle
rotY = newAngle - angle;
//spinning the garden
transform.RotateAround(Vector3.zero, Vector3.up, rotX);
transform.RotateAround(Vector3.zero, transform.TransformDirection(Vector3.right), -rotY); //vertical rotation
If anyone knows of the correct way to make this work for the Y axis, or a different way to clamp the vertical rotation, I would be super excited to hear it! Ty!
I have a class here that do exactly what you want. It rotates a camera around a target and clamps the Y rotation. It uses the left button to rotate and the scroll press button to translate the target.
You can edit it to adjust to your specific needs - you might want to change the target to a Vector3 so you can set it to (0,0,0) without the need of an object. Hope it helps.
using UnityEngine;
public class RotateAroundCamera : MonoBehaviour
{
Camera cam;
public bool isControlable;
private Vector3 screenPoint;
private Vector3 offset;
public Transform target;
public float distance = 5.0f;
public float xSpeed = 50.0f;
public float ySpeed = 50.0f;
public float yMinLimit = -80f;
public float yMaxLimit = 80f;
public float distanceMin = .5f;
public float distanceMax = 15f;
public float smoothTime = 2f;
public float rotationYAxis = 0.0f;
float rotationXAxis = 0.0f;
float velocityX = 0.0f;
float velocityY = 0.0f;
float moveDirection = -1;
public void SetControllable(bool value)
{
isControlable = value;
}
// Use this for initialization
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(1) && isControlable)
{
velocityX += xSpeed * Input.GetAxis("Mouse X") * 0.02f;
velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
}
if (Input.GetMouseButton(2) && isControlable)
{
Vector3 curScreenPoint = new Vector3(moveDirection*Input.mousePosition.x, moveDirection*Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = cam.ScreenToWorldPoint(curScreenPoint) + offset;
target.transform.position = curPosition;
}
if (Input.GetKeyDown(KeyCode.R) && isControlable)
{
target.transform.position = Vector3.zero;
}
if (Input.GetKeyDown(KeyCode.T) && isControlable)
{
moveDirection *= -1;
}
if (isControlable)
{
distance -= Input.GetAxis("Mouse ScrollWheel");
if (distance > distanceMax)
{
distance = distanceMax;
}
else if (distance < distanceMin)
{
distance = distanceMin;
}
}
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.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);
}
}

Panning not working within my camera script

I've got a camera script that I'm working on and I just cannot get the camera pan to work at all. It's recognising the input, but it's not moving the camera at all.
It was working at one point but it reset the camera upon zooming or rotating, now the zooming and rotating work perfectly. This issue was due to the function PositionRotation which you can find at the bottom.
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
[AddComponentMenu("Camera-Control/Mouse drag Orbit with zoom")]
public class DragMouseOrbit : MonoBehaviour
{
public Transform target;
public float distance = 5.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
public float distanceMin = .5f;
public float distanceMax = 15f;
public float smoothTime = 2f;
public float zoomFactor = 5f;
public float panFactor = 10f;
private Transform m_Transform;
float rotationYAxis = 0.0f;
float rotationXAxis = 0.0f;
float velocityX = 0.0f;
float velocityY = 0.0f;
// Use this for initialization
void Start()
{
Vector3 angles = transform.eulerAngles;
rotationYAxis = angles.y;
rotationXAxis = angles.x;
// Make the rigid body not change rotation
if (GetComponent<Rigidbody>())
{
GetComponent<Rigidbody>().freezeRotation = true;
}
}
void Update()
{
if (!EventSystem.current.IsPointerOverGameObject())
{
if(Input.GetMouseButton(0))
{
CameraRotate();
}
if(Input.GetMouseButton(1))
{
CameraPan();
}
if(Input.GetAxis("Mouse ScrollWheel") <= 0)
{
CameraZoom();
}
if (Input.GetAxis("Mouse ScrollWheel") >= 0)
{
CameraZoom();
}
}
}
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);
}
public void CameraRotate()
{
if (target)
{
velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
}
rotationYAxis += velocityX;
rotationXAxis -= velocityY;
rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
PositionRotation();
}
public void CameraPan()
{
if (!EventSystem.current.IsPointerOverGameObject())
{
Debug.Log("right click has been pressed dumbfuck");
//transform.rotation = transform.rotation;
transform.Translate(Vector3.right * -Input.GetAxis("Mouse X") * panFactor);
transform.Translate(Vector3.forward * -Input.GetAxis("Mouse Y") * panFactor);
}
}
public void CameraZoom()
{
if (!EventSystem.current.IsPointerOverGameObject())
{
//transform.Translate(Vector3.forward * +Input.GetAxis("Mouse ScrollWheel") * zoomFactor);
PositionRotation();
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * zoomFactor, distanceMin, distanceMax);
RaycastHit hit;
if (Physics.Linecast(target.position, transform.position, out hit))
{
distance -= hit.distance;
}
}
}
void PositionRotation()
{
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.position;
transform.rotation = rotation;
transform.position = position;
velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
}
}
The problem is within your Update function. It calls CameraZoom all the time because you set
Input.GetAxis("Mouse ScrollWheel") <= 0
And CameraZoom calls PositionRotation, where you reset the positon of the camera to the original position.
Try setting it to
if (Input.GetAxis("Mouse ScrollWheel") <= -0.1)
{
CameraZoom();
}
if (Input.GetAxis("Mouse ScrollWheel") >= 0.1)
{
CameraZoom();
}
That should do the trick.
Cheers
Tobi

Swipe screen to rotate camera

I am writing a 3D game for smartphone using Unity3D and have no idea on making camera rotation (First Person Perspective).
Making virtual joystick is too hard, so I decided to make camera rotate on screen swipe.
User swipe smartphone's screen and camera turns around.
Rotating works, but always starts from the same position.
Saving last position breaks all rotating (rotating is askew).
My code:
using UnityEngine;
using System.Collections;
public class Look : MonoBehaviour {
public float sensitivityX = 1F;
public float sensitivityY = 1F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -25F;
public float maximumY = 25F;
float rotationX = 0F;
float rotationY = 0F;
float oldRotationX = 0F;
float oldRotationY = 0F;
float lastX = 0F;
float lastY = 0F;
Quaternion originalRotation;
void Update ()
{
if (Input.touches.Length > 0)
{
if(Input.touches[0].phase == TouchPhase.Began)
{
lastX = Input.touches[0].position.x;
lastY = Input.touches[0].position.y;
rotationX = transform.localEulerAngles.x;
rotationY = transform.localEulerAngles.y;
oldRotationX = rotationX;
oldRotationY = rotationY;
}
if(Input.touches[0].phase == TouchPhase.Moved)
{
rotationX = (oldRotationX + (Input.touches[0].position.x - lastX)) * sensitivityX;
rotationY = (oldRotationY + (Input.touches[0].position.y - lastY)) * sensitivityY;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
rotationY = ClampAngle (rotationY, minimumY, maximumY);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, -Vector3.right);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
//this should make rotating from last position, but it
//makes rotating incorrect (askew)
//without code below rotating works, but on always
//starts from the same position
if(Input.touches[0].phase == TouchPhase.Ended)
{
originalRotation = transform.localRotation;
}
}
}
void Start ()
{
if (rigidbody)
rigidbody.freezeRotation = true;
originalRotation = transform.localRotation;
}
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);
}
}
Please help me. Thanks.
You can use the inputs, specifically the Touch positions. You can then apply a similar effect achieved in a mouse look script.
Essentially the code you want is:
rotationX += Input.touches[0].position.x * sensitivityX;
rotationY += Input.touches[0].position.y * sensitivityY
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, -Vector3.right);
transform.localRotation *= xQuaternion * yQuaternion;
if(Input.touches.Length > 0 && Input.GetTouch(0).phase == TouchPhase.Moved){
transform.Rotate(new Vector3(-Input.GetTouch(0).deltaPosition.y,Input.GetTouch(0).deltaPosition.x,0)*speed*Time.deltaTime);
transform.rotation=Quaternion.Euler(transform.rotation.eulerAngles.x,transform.rotation.eulerAngles.y,0);
}

Categories