I’m trying to use the joystick on mobile to move the camera on the XZ axis. (top down game)
The Vector2 in Unity defaults to the XY axis. I can only move the X axis, because the Y axis moves the camera downwards.
Is there a way to change the default XY axis into XZ axis?
Maybe I could hack into the Mouse XY inputs somehow? I don’t even know where those files are located tho.
Here is my camera/joystick script:
using UnityEngine;
using System.Collections;
public class UIJoystick : MonoBehaviour
{
// draggable target object
public Transform target;
public float radius = 50f;
public Vector2 position; // [-1, 1] in x,y
public float speed;
private Vector3 initPos;
private Vector3 initRot;
private Transform cam;
//rotation variables
public float xRotSpeed = 200f;
public float yRotSpeed = 200f;
public float rotDamp = 5f;
public float yMinRotLimit = -10f;
public float yMaxRotLimit = 60f;
private float xDeg; //Mouse X axis input var
private float yDeg; //Mouse Y axis input var
private Quaternion desiredRotation; //returned rotation based on xDeg and yDeg
private Quaternion currentRotation; //current camera rotation
private Quaternion rotation; //rotation with damping ( final rotation )
void Start()
{
cam = Camera.main.transform; //get camera transform
initPos = cam.position; //get starting position
initRot = cam.eulerAngles; //get starting rotation
//be sure to grab the current rotations as starting points.
rotation = currentRotation = desiredRotation = cam.rotation;
//grab current angle values as starting points, clamp to return positive values
xDeg = ClampAngle(cam.eulerAngles.y, 0f, 360f);
yDeg = ClampAngle(cam.eulerAngles.x, 0f, 360f);
}
void LateUpdate()
{
if (!SV.control)
{
//limit movement to be within the scene bounds
if (CheckBounds())
cam.Translate(new Vector3(position.x, 0, position.y) * speed);
//get current camera position and calculate new height position based on old positions
Vector3 pos = cam.position;
pos.y -= (cam.position.y - initPos.y) * 0.1f;
cam.position = pos;
//keep initial rotation
xDeg = initRot.y;
yDeg = initRot.x;
}
else
{
Rotate();
}
}
//actual target drag method
void OnDrag (Vector2 delta)
{
//use NGUI 2D camera to find the last touch position
Ray ray = UICamera.currentCamera.ScreenPointToRay(UICamera.lastTouchPosition);
//store last touch position as Vector3
Vector3 currentPos = ray.origin;
//we want to use the touch position in 2D, so zero out its depth value
currentPos.z = 0f;
//set the joystick thumb to touched position
target.position = currentPos;
//length of the touch vector (magnitude)
//calculated from the relative position of the joystick thumb
float length = target.localPosition.magnitude;
//if the thumb leaves the joystick's boundaries,
//clamp it to the max radius
if (length > radius)
{
target.localPosition = Vector3.ClampMagnitude(target.localPosition, radius);
}
//set the Vector2 thumb position based on the actual sprite position
position = target.localPosition;
//smoothly lerps the Vector2 thumb position based on the old positions
position = position / radius * Mathf.InverseLerp (radius, 2, 1);
}
//check if the drag is over
public void OnPress(bool pressed)
{
//we aren't dragging anymore,
//set the joystick back to the default position
if (!pressed)
{
position = Vector2.zero;
target.position = transform.position;
}
}
void Rotate()
{
//Set the current joystick input variables multiplied by speed and a slowing factor
xDeg += position.x * xRotSpeed * 0.02f;
yDeg -= position.y * yRotSpeed * 0.02f;
//clamp the angle between min and max rotation
yDeg = ClampAngle(yDeg, yMinRotLimit, yMaxRotLimit);
//set camera rotation
//this returns a rotation that rotates yDeg degrees around x-axis and xDeg degrees around the y-axis
desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
//get current rotation of camera transform
currentRotation = cam.rotation;
//Add damping factor, smoothly rotate from currentRotation to desired location in time with that damping
rotation = Quaternion.Lerp(currentRotation, desiredRotation, Time.deltaTime * rotDamp);
//assign final rotation to camera
cam.rotation = rotation;
}
//clamps the angle to always return positive values
//and make sure it never exceeds given limits
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);
}
//this method casts a ray against world limit mask with length of 5 units and returns a boolean,
//which indicates whether movement is possible in that direction
//on hit: world limit reached - return false, no hit: free space - return true
private bool CheckBounds()
{
Vector3 forw = cam.forward * position.y;
Vector3 side = cam.right * position.x;
if (Physics.Raycast(cam.position, forw, 5, SV.worldMask))
{
return false;
}
else if (Physics.Raycast(cam.position, side, 5, SV.worldMask))
return false;
return true;
}
//visible gizmo lines in editor for each direction of the camera, with length of 5 units
//so we see if we touched a movement limit
void OnDrawGizmos()
{
//draw gizmos in blue color
Gizmos.color = Color.cyan;
if (cam)
{
Gizmos.DrawRay(cam.position, cam.forward * position.y * 5);
Gizmos.DrawRay(cam.position, cam.right * position.x * 5);
}
}
}