How to make A Joystick Movement Controller that moves the character based on the camera Angle (ARCore)
Hi!
I am working in a ARCore Game project. In a scene, Where user will spawn the Playable Character, I want the user to be able to move the character using a joystick. But the catch is, since I am making it with Augmented reality, that means User will move around the area with their phone.
So, I want the joy stick to work such that, if the User Pushes "Vertical Up" the character should always walk Away from the camera. In other worlds I want the joystick to take the Phones's camera Origin and Orientation as its parent rather than taking the Game-Scene's Origin as Parent when it comes to defining X, Y and Z Axis.
Also I have added a second Joystick that user can use to rotate the Character around the Character's axis to shoot enemies at 360 degrees around it.
I found "THIS" tutorial that I Implemented. When I tested it, the object is moving and rotating just fine, But it is not moving the way I mentioned above. It seems to go forward in the same direction regardless of where I position the camera.
Here's the Scripts I have written (Movement_Controller.cs):
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement_Controller : MonoBehaviour
{
public FixedJoystick moveJoystick;
GameObject objMove_Joystick;
public FixedJoystick LookJoystick;
GameObject objLook_Joystick;
private static float hoz;
private static float ver;
Vector3 direction;
void Update()
{
if (NewTap2Place.objectinScene == true)
{
MoveJoystick();
look_JoyStick();
}
}
void MoveJoystick()
{
objMove_Joystick = GameObject.FindGameObjectWithTag("Movement_Stick") as GameObject;
moveJoystick = objMove_Joystick.GetComponent<FixedJoystick>();
hoz = moveJoystick.Horizontal;
ver = moveJoystick.Vertical;
Vector3 convertedXY = ConvertWithCamera(Camera.current.transform.position, hoz, ver);
direction = new Vector3(convertedXY.x, 0, convertedXY.y);
//Debug.Log(direction);
}
void look_JoyStick()
{
objLook_Joystick = GameObject.FindGameObjectWithTag("Look_Stick") as GameObject;
LookJoystick = objLook_Joystick.GetComponent<FixedJoystick>();
hoz = LookJoystick.Horizontal;
ver = LookJoystick.Vertical;
Vector3 convertedXY = ConvertWithCamera(Camera.current.transform.position, hoz, ver);
direction = new Vector3(convertedXY.x, 0, convertedXY.y);
Vector3 LookatPosition = transform.position + direction;
transform.LookAt(LookatPosition);
//Debug.Log(direction);
}
private Vector2 ConvertWithCamera(Vector3 cameraPos, float hor, float ver)
{
Vector2 joyDirection = new Vector2(hor, ver).normalized;
Vector2 camera2Pos = new Vector2(cameraPos.x, cameraPos.y);
Vector2 cameraToPlayerDirection = (Vector2.zero - camera2Pos).normalized;
float angle = Vector2.SignedAngle(cameraToPlayerDirection, new Vector2(0, 1));
Vector2 finalDirection = RotateVector(joyDirection, -angle);
return finalDirection;
}
private Vector2 RotateVector(Vector2 v, float angle)
{
float radian = angle * Mathf.Deg2Rad;
float _x = v.x * Mathf.Cos(radian) - v.y * Mathf.Sin(radian);
float _y = v.x * Mathf.Sin(radian) + v.y * Mathf.Cos(radian);
return new Vector2(_x, _y);
}
}
I am using Unity: 2019.4, ARFoundation: 4.1.0 I have also tagged the ARCamera as Main Camera in its inspector.
Also I am instantiating the object after the user taps on the screen, does that affect the coordinate system?
Thanks in advance!