Currently, I have a set up where the character I am controlling moves forward fine, but attempting to go any other direction causes problems.
The goal here is to get the character to face the direction the joystick is pointed (it moves just fine, as you can see from the gifs)
Here are my scripts
Player motion
using UnityEngine;
using System.Collections;
public class Motion : MonoBehaviour {
Animator anim;
Rigidbody rig;
Camera cam;
void Awake(){
anim = GetComponent <Animator> ();
rig = GetComponent<Rigidbody> ();
cam = Camera.main;
}
void Update () {
float horizontal = Input.GetAxis ("Horizontal");
float vertical = Input.GetAxis ("Vertical");
float joystickMagnitude = Mathf.Clamp01 (new Vector2 (horizontal, vertical).magnitude);
float angle = Mathf.Atan2 (horizontal, vertical) * Mathf.Rad2Deg;
anim.SetFloat ("Speed", joystickMagnitude);
Vector3 movement = new Vector3 (0, Mathf.Atan2(horizontal, vertical), 0);
rig.transform.Rotate(movement);
if (Input.GetButtonDown ("Sprint")) {
anim.Play ("sprinting_forward_roll");
}
}
}
Camera Script
using UnityEngine;
using System.Collections;
public class thirdPersonCamera : MonoBehaviour {
public Transform lookAt;
public Transform camTransform;
private const float yAngleMin = -15.0f;
private const float yAngleMax = 40.0f;
private float distance = 2.0f;
private float currentX = 0.0f;
private float currentY = 0.0f;
private float xSensitivity = 4.0f;
private float ySensitivity = 2.0f;
public void Start(){
camTransform = transform;
}
public void Update(){
currentX += Input.GetAxis("Mouse X") * xSensitivity;
currentY += Input.GetAxis("Mouse Y") * ySensitivity;
currentY = Mathf.Clamp (currentY, yAngleMin, yAngleMax);
}
public void LateUpdate(){
Vector3 direction = new Vector3 (0, 0, -distance);
Quaternion rotation = Quaternion.Euler (currentY, currentX, 0);
camTransform.position = lookAt.position + rotation * direction;
camTransform.LookAt (lookAt.position);
}
}
There is nothing wrong with the camera script. In fact, it is exactly how I want it for right now. What I would like to figure out is how to make the player face the direction the joystick is pointed, and if possible (although not necessarily) relative to the camera. I can probably figure out the camera relative part on my own once I figure out how to fix the motion script, although help is appreciated with that.
What should I do? because clearly the solution of Mathf.Atan2(horizontal, vertical) is not working in this particular situation.