Joystick intercalate with camera rotation - unity3d

I make game with fps , when I rotate the camera the joystick doesn't work properly. I have a fixed Joystick , when I try to move by rotation anything in movement change . Do I need to change the joystick ? or do I need to change something in the script for the camera rotation ? When I start the game on the mobile , the rotation affects the joystick . When I move the joystick forward and do some rotation , forward becomes backward , left right , if I move right the rotation , right becomes backward , etc . what can I do
using System.Collections.Generic;
using UnityEngine;
[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour
{
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
float turnAngleChange = (touch.deltaPosition.x / Screen.width) * sensitivityX;
float pitchAngleChange = (touch.deltaPosition.y / Screen.height) * sensitivityY;
// Handle any pitch rotation
if (axes == RotationAxes.MouseXAndY || axes == RotationAxes.MouseY)
{
rotationY = Mathf.Clamp(rotationY + pitchAngleChange, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0f);
}
// Handle any turn rotation
if (axes == RotationAxes.MouseXAndY || axes == RotationAxes.MouseX)
{
transform.Rotate(0f, turnAngleChange, 0f);
}
}
void Start()
{
//if(!networkView.isMine)
//enabled = false;
// Make the rigid body not change rotation
//if (rigidbody)
//rigidbody.freezeRotation = true;
}
}
}

Related

How to detect finger drag on either x or y axis for mobile?

Anyway to get or recreate this type of input control for mobile touch, I essentially instead of detecting mouse drag on the y I want to detect finger drag on the y, and well honestly x too? Would I have to do this via script and if so what would the code be for that?
Input Manager Mouse Y, but I want finger Y ha :'D
This code is for both drag(pan) and zooming.
I used this code for my game
using UnityEngine;
using UnityEngine.EventSystems;
public class PanZoom : MonoBehaviour
{
Vector3 touchStart;
// Minimum amount pan to move camera
public float panMinX = 2f;
public float panMinY = 2f;
// Minimum and Maximum zoom amount for camera
public float zoomOutMin = 5f;
public float zoomOutMax = 20f;
// Scroll speed of zoom of camera
public float scrollSpeed = 5f;
void Update()
{
if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
{
touchStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
#if UNITY_STANDALONE || UNITY_EDITOR || UNITY_WEBGL
if (Input.GetMouseButton(0) && !EventSystem.current.IsPointerOverGameObject())
{
Vector3 direction = touchStart - Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (Mathf.Abs(direction.x) > panMinX || Mathf.Abs(direction.y) > panMinY)
if (!CameraMovement.Instance.IsCameraInBorder)
{
Camera.main.transform.position += direction;
}
}
Zoom(Input.GetAxis("Mouse ScrollWheel") * scrollSpeed);
#endif
#if UNITY_ANDROID || UNITY_IOS
if (Input.touchCount == 2 && !EventSystem.current.IsPointerOverGameObject())
{
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
Vector2 touchZeroPrevPosition = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPosition = touchOne.position - touchOne.deltaPosition;
float prevMagnitude = (touchZeroPrevPosition - touchOnePrevPosition).magnitude;
float currentMagnitude = (touchZero.position - touchOne.position).magnitude;
float difference = currentMagnitude - prevMagnitude;
Zoom(difference * 0.01f);
}
else if (Input.GetMouseButton(0) && !EventSystem.current.IsPointerOverGameObject())
{
Vector3 direction = touchStart - Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (Mathf.Abs(direction.x) > panMinX || Mathf.Abs(direction.y) > panMinY)
Camera.main.transform.position += direction;
}
#endif
}
void Zoom(float increment)
{
if(!EventSystem.current.IsPointerOverGameObject())
Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize - increment, zoomOutMin, zoomOutMax);
}
}

Joystick controls mixed with rotation

I have a problem with the Fixed joystick when I apply rotation to the main camera. The controls are mixed. How can I resolve this problem? Do I need to use other type of joystick? Where I should put "-" ? in front of which number
using System.Collections.Generic;
using UnityEngine;
[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour
{
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
float turnAngleChange = (touch.deltaPosition.x / Screen.width) * sensitivityX;
float pitchAngleChange = (touch.deltaPosition.y / Screen.height) * sensitivityY;
// Handle any pitch rotation
if (axes == RotationAxes.MouseXAndY || axes == RotationAxes.MouseY)
{
rotationY = Mathf.Clamp(rotationY + pitchAngleChange, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0f);
}
// Handle any turn rotation
if (axes == RotationAxes.MouseXAndY || axes == RotationAxes.MouseX)
{
transform.Rotate(0f, turnAngleChange, 0f);
}
}
void Start()
{
//if(!networkView.isMine)
//enabled = false;
// Make the rigid body not change rotation
//if (rigidbody)
//rigidbody.freezeRotation = true;
}
}
}
I don't really get your question so I'm gonna answer both of my interpretations.
If the rotation of the camera is inverted (if you put your stick to the left the camera rotates to the right) you just need to add a minus in front of the speed you're rotating the camera with.
If the rotation of the camera happens when you use the left stick and you want to use the right stick. Then you should define in your input a field called something like camera-rotation-x and use the axis shown on the following pictures in the article here: https://gamedev.stackexchange.com/questions/150323/unity3d-how-to-use-controller-joystick-to-look-around If you follow that article, your camera rotation should work.

Rotation on touch with gyroscope in a mobile device behaving in a weird manner

I have been using the gvr sdk in my project to get a 360 view during the video playback through the camera. But when I rotate the camera on touch, the rotation itself is behaving in a weird manner i.e. during the landscape mode, rotation in y-axis is working fine but when the gyroscope is moved towards the right or left, the x-axis rotation is behaving as z-axis rotation. Please help. Below is the code for simple rotation in x and y axis using a touch input on the device.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RCPlayer : MonoBehaviour
{
public static RCPlayer Instance;
//public GameObject Head;
//public GameObject Camera;
Vector3 FirstPoint;
Vector3 SecondPoint;
float xAngle;
float yAngle;
float xAngleTemp;
float yAngleTemp;
void Awake()
{
Instance = this;
}
void Start()
{
xAngle = 0;
yAngle = 0;
transform.rotation = Quaternion.Euler(yAngle, xAngle, 0);
}
void Update()
{
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
FirstPoint = Input.GetTouch(0).position;
xAngleTemp = xAngle;
yAngleTemp = yAngle;
}
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
SecondPoint = Input.GetTouch(0).position;
if (FirstPoint - SecondPoint == Vector3.zero)
{
return;
}
else
{
xAngle = xAngleTemp + (SecondPoint.x - FirstPoint.x) * 180 / Screen.width;
yAngle = yAngleTemp - (SecondPoint.y - FirstPoint.y) * 180 / Screen.height;
transform.rotation = Quaternion.Euler(-yAngle * 0.5f, -xAngle * 0.5f, 0.0f);
}
}
}
}
}

How do I make the border of a map and the panning of that map the same?

I am using a map bigger than the screen I viewing. Therefore I need to be able to pan around that map. I am having a problem with clamping the camera and the map. I want to be able to use the demension of the image as the width and height of the clamp. The problem is the units.
The image is 2144 x 1708
The camera transposition is in single digits (14 x 7) or something like that.
All of the code I am using is below.
private Vector3 mouseOrigin; // Position of cursor when mouse dragging starts
private bool isPanning; // Is the camera being panned?
public bool useBoundary = true;
public Vector2 boundaryMin;
public Vector2 boundaryMax;
public Image map;
private void Start()
{
Camera cam = Camera.main;
float mapRatio = map.rectTransform.rect.width / map.rectTransform.rect.height;
float mapScreenHeight = (1.5f * cam.orthographicSize);
float mapScreenWidth = (3f * mapScreenHeight) * cam.aspect;
boundaryMin = new Vector2(0, 1);
boundaryMax = new Vector2(map.rectTransform.rect.width, map.rectTransform.rect.height);
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
mouseOrigin = Input.mousePosition;
isPanning = true;
}
// Disable movements on button release
if (!Input.GetMouseButton(0))
isPanning = false;
if (isPanning)
{
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
Vector3 move = new Vector3(pos.x * panSpeed, pos.y * panSpeed, 0);
transform.Translate(move, Space.Self);
BoundaryCheck();
}
}
private void BoundaryCheck()
{
if (!useBoundary)
return;
Vector3 newPos = transform.position;
newPos.x = Mathf.Clamp(newPos.x, boundaryMin.x, boundaryMax.x);
newPos.y = Mathf.Clamp(newPos.y, boundaryMin.y, boundaryMax.y);
transform.position = newPos;
}
}
Any help would be greatly appreciated.
You can do this using Unity UI - Scroll Rect.
Check out Unity UI - Scroll Rect - Introduction
In my opinion you should need to use below script for camera panning, zooming and rotating. You can off any undesirable function. Now according to your question you have to restrict the movement(according to your image border). simply you can restrict the camera movements on differet axis according to your limitations. You should place cubes into the borders of the map and use their positions as the limit of camera movement and panning etc.
using UnityEngine;
using System.Collections;
public class CamMovementManager : MonoBehaviour
{
#region Vars
public float turnSpeed = 1.0f; // Speed of camera turning when mouse moves in along an axis
public float panSpeed = 4.0f; // Speed of the camera when being panned
public float zoomSpeed = 4.0f; // Speed of the camera going back and forth
private Vector3 mouseOrigin; // Position of cursor when mouse dragging starts
private bool isPanning; // Is the camera being panned?
private bool isRotating; // Is the camera being rotated?
private bool isZooming; // Is the camera zooming?
private float pannPosLimit = 300f;
private int fovMin = 15;
private int fovMax = 90;
#endregion Vars
#region UnityEvents
void Update()
{
// Get the left mouse button
if (Input.GetMouseButtonDown(0))
{
// Get mouse origin
mouseOrigin = Input.mousePosition;
isRotating = true;
}
// Get the right mouse button
if (Input.GetMouseButtonDown(1))
{
// Get mouse origin
mouseOrigin = Input.mousePosition;
isPanning = true;
}
// Get the middle mouse button
if (Input.GetMouseButtonDown(2))
{
// Get mouse origin
//mouseOrigin = Input.mousePosition;
//isZooming = true;
}
//changing fov on mouse scroll to zoomIn/out
float fov = Camera.main.fieldOfView;
fov += Input.GetAxis("Mouse ScrollWheel") * 10f;
fov = Mathf.Clamp(fov, fovMin, fovMax);
Camera.main.fieldOfView = fov;//*/
// Disable movements on button release
if (!Input.GetMouseButton(0)) isRotating = false;
if (!Input.GetMouseButton(1)) isPanning = false;
if (!Input.GetMouseButton(2)) isZooming = false;
// Rotate camera along X and Y axis
if (isRotating)
{
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
//Debug.Log("rotate pos : " + pos);
transform.RotateAround(transform.position, transform.right, -pos.y * turnSpeed);
transform.RotateAround(transform.position, Vector3.up, pos.x * turnSpeed);
}
// Move the camera on it's XY plane
if (isPanning)
{
if (Input.mousePosition.y > pannPosLimit)
{
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
Vector3 move = new Vector3(pos.x * panSpeed, pos.y * panSpeed, 0);
transform.Translate(move, Space.Self);
}
}
// Move the camera linearly along Z axis
if (isZooming)
{
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
Vector3 move = pos.y * zoomSpeed * transform.forward;
transform.Translate(move, Space.World);
}
}
#endregion
#region CustomMethods
public void CamRotating()
{
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
transform.RotateAround(transform.position, transform.right, -pos.y * turnSpeed);
transform.RotateAround(transform.position, Vector3.up, pos.x * turnSpeed);
}
public void CamPanning()
{
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
Vector3 move = new Vector3(pos.x * panSpeed, pos.y * panSpeed, 0);
transform.Translate(move, Space.Self);
}
public void CamZooming()
{
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
Vector3 move = pos.y * zoomSpeed * transform.forward;
transform.Translate(move, Space.World);
}
#endregion CustomMethods
}

How to Move an Object On Y Axis with Mouse in Unity

I want control the transform with mouse just for Y axis as it is moving on X axis by it self.
I use Vector2.MoveTowards to move it on X axis.
The transform must be moved by any touch and drag on the screen. So I wrote my codes in the Update function
My Codes :
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
public Camera playerCamera;
public float speed = 12.0F;
private Vector3 moveDirection = Vector3.zero,Point,last;
void Start()
{
Point = transform.position;
last = transform.position;
if (playerCamera == null)
{
playerCamera = Camera.main;
}
playerCamera.transparencySortMode = TransparencySortMode.Orthographic;
}
void Update(){
if(Input.GetMouseButtonUp(0))
Point = last;
moveDirection.x = transform.position.x+speed;
moveDirection.y = transform.position.y;
if (Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
last = ray.GetPoint (0);
transform.position = new Vector2 (transform.position.x, last.y - Point.y);
}
else
transform.position = Vector2.MoveTowards( transform.position,moveDirection,speed*Time.deltaTime);
//moveDirection.y -= 20.0f * Time.smoothDeltaTime;
//After we move, adjust the camera to follow the player
playerCamera.transform.position = new Vector3(transform.position.x, playerCamera.transform.position.y , playerCamera.transform.position.z);
}
}
How can I solve it?