- Home /
The question is answered, right answer was accepted
Android deltaPosition.y isn't working?
I've got a weird bug in my project.
A little background, I have a joystick for movement on the left side of the screen, and I want the player to be able to rotate the camera (mouse/touch orbit) around the target using only the right side of the screen, so there would be no collision with the left joystick and the calculation of the position.
The code works great when I test in Unity Remote, but when I build it, it somewhat seems that the y position of the Mouse Orbit is locked, and I'm unable to change the height position of the camera.
Here is the code: using UnityEngine; using System.Collections;
[AddComponentMenu("Camera-Control/Mouse Orbit with zoom")]
public class MouseOrbit : 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;
float x = 0.0f;
float y = 0.0f;
// Use this for initialization
void Start()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
void Update()
{
if (target && Input.touchCount == 1 && Input.GetTouch(0).position.x > Screen.width / 2 && Input.GetTouch(0).phase == TouchPhase.Moved) //Just orbit touch without movement
{
Debug.Log("Orbiting! 1 touch");
Orbit(Input.GetTouch(0));
}
else if (Input.touchCount == 2)
{
if (Input.GetTouch(0).position.x > Screen.width / 2 && Input.GetTouch(0).phase == TouchPhase.Moved)
Orbit(Input.GetTouch(0)); //Movement was touched second
else if (Input.GetTouch(1).position.x > Screen.width / 2 && Input.GetTouch(1).phase == TouchPhase.Moved)
Orbit(Input.GetTouch(1)); //Movement was touched first
}
}
void Orbit(Touch touch)
{
x += touch.deltaPosition.x * xSpeed * 0.02f /* * distance*/;
y -= touch.deltaPosition.y * ySpeed * 0.02f /* * distance*/;
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
//distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
RaycastHit hit;
if (Physics.Linecast(target.position, transform.position, out hit))
{
distance -= hit.distance;
}
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
}
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);
}
}
Answer by Ranger-Ori · Oct 07, 2013 at 08:29 AM
The issue was fixed, there was a collision between two scripts, MouseOrbit.js and MouseOrbit.cs, apparently there was a warning on the console which I didn't pay attention to it since I thought it was a minor issue, and as I said, the game worked just fine with Unity Remote, after creating a new script "TouchOrbit.cs" and using it only, there was no collision and the script worked excellently on the mobile device.
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Camera performance issue 0 Answers
Cinemachine FreeLook - Explicitly setting orbit rotation 1 Answer
Game preview incorrect! 0 Answers
Camera Changes Proportions In Build 1 Answer