Face direction of a Vector 3
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.
Answer by Otaku_Riisu · Jun 24, 2016 at 12:59 AM
I solved the issue on my own
//Gets the direction in which the camera is facing
Vector3 cameraDirection = cam.forward;
//This Vector 3 represents a dead joystick, meaning it is not being pushed in any direction
Vector3 deadJoystick = new Vector3(0,0,0);
//The Vector 3 here gets the direction in which the joystick is being pushed on the controller
Vector3 stickDirection = new Vector3 (horizontal, 0, vertical);
//keeps the camera's y value the same as that of the stickDirection for clean calculations
cameraDirection.y = 0.0f;
//Adjusts the direction of motion based on the position of the camera
Quaternion referentialShift = Quaternion.FromToRotation (Vector3.forward, cameraDirection);
//Changes the angle of stickDirection to be camera-relative
Vector3 moveDirection = referentialShift * stickDirection;
//If the joystick is not being pressed, don't do anything
if (moveDirection == deadJoystick) return;
//rotate the player in the direction of the joystick
rig.transform.rotation = Quaternion.LookRotation (moveDirection.normalized);
The rotation is slightly clunky, but it is easy enough to deal with for now. I hope this helped someone!