In my game there are 2 joysticks one for the player movement and one for the camera movement when i change the camera angle with the camera joystick and move the player in a direction with the player joystick the player moves in another direction what should i do?`
`Here is the camera script
using UnityEngine;
using System.Collections;
public class FreeCamera : MonoBehaviour {
public Transform lookAt;
public VirtualJoystick camerajs;
private float distance = 10.0f;
private float currentx = 0.0f;
private float currenty = 0.0f;
private float sensitivityx = 3.0f;
private float sensitivityy = 1.0f;
private void Update()
{
currentx += camerajs.InputDirection.x * sensitivityx;
currenty += camerajs.InputDirection.z * sensitivityy;
}
private void LateUpdate()
{
Vector3 dir = new Vector3(0, 0, -distance);
Quaternion rotation = Quaternion.Euler(currenty, currentx, 0);
transform.position = lookAt.position + rotation * dir;
transform.LookAt(lookAt);
}
}
And here is the joystick script
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
using UnityEngine.UI;
public class VirtualJoystick : MonoBehaviour,IDragHandler,IPointerUpHandler,IPointerDownHandler {
private Image bgimg;
private Image joystickimg;
public Vector3 InputDirection { set; get; }
private void Start()
{
bgimg = GetComponent<Image>();
joystickimg = transform.GetChild(0).GetComponent<Image>();
InputDirection = Vector3.zero;
}
public virtual void OnDrag(PointerEventData ped)
{
Vector2 pos = Vector2.zero;
if(RectTransformUtility.ScreenPointToLocalPointInRectangle
(bgimg.rectTransform,
ped.position,
ped.pressEventCamera,
out pos))
{
pos.x = (pos.x / bgimg.rectTransform.sizeDelta.x);
pos.y = (pos.y / bgimg.rectTransform.sizeDelta.y);
float x = (bgimg.rectTransform.pivot.x == 1) ? pos.x * 2 + 1 : pos.x * 2 - 1;
float y = (bgimg.rectTransform.pivot.y == 1) ? pos.x * 2 + 1 : pos.y * 2 - 1;
InputDirection = new Vector3(x, 0, y);
InputDirection = (InputDirection.magnitude > 1) ? InputDirection.normalized : InputDirection;
joystickimg.rectTransform.anchoredPosition =
new Vector3(InputDirection.x * (bgimg.rectTransform.sizeDelta.x / 3)
, InputDirection.z * (bgimg.rectTransform.sizeDelta.y / 3));
}
}
public virtual void OnPointerDown(PointerEventData ped)
{
OnDrag(ped);
}
public virtual void OnPointerUp(PointerEventData ped)
{
InputDirection = Vector3.zero;
joystickimg.rectTransform.anchoredPosition = Vector3.zero;
}
}
The player movement script
using UnityEngine;
using System.Collections;
public class Motor : MonoBehaviour {
public float moveSpeed = 5.0f;
public float drag = 0.5f;
public float terminalRotationSpeed = 25.0f;
private Rigidbody controller;
private Transform camtransform;
public VirtualJoystick movejoystick;
private void Start()
{
controller =GetComponent<Rigidbody>();
controller.maxAngularVelocity = terminalRotationSpeed;
controller.drag = drag;
camtransform = Camera.main.transform;
}
private void Update()
{
Vector3 dir = Vector3.zero;
dir.x = Input.GetAxis("Horizontal");
dir.z = Input.GetAxis("Vertical");
if (dir.magnitude > 1)
dir.Normalize();
if (movejoystick.InputDirection != Vector3.zero)
{
dir = movejoystick.InputDirection;
}
Vector3 rotatedDir = camtransform.TransformDirection(dir);
rotatedDir = new Vector3 (rotatedDir.x, 0, rotatedDir.z);
rotatedDir = rotatedDir.normalized * dir.magnitude;
controller.AddForce(dir * moveSpeed);
}