- Home /
Joystick Rotating wrong after Camera Rotation [Mobile]
I use one joytick to have my player rotating and moving using the Controller.cs script below and I'm using SmoothFollow.cs from the assets to get the camera movement to follow the player the way I want it, however, when I press my button to move the camera behind my player, the rotation corresponding to joystick is off. Can someone help me find what I missing?
using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
public class Controller : MonoBehaviour {
public float boostMultiplier =2;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
bool isBoosting = CrossPlatformInputManager.GetButton ("Boost");
Vector3 moveVec = new Vector3 (0, 0, 1);
//This is where joystick rotates player
if ( CrossPlatformInputManager.GetAxis("Vertical")>0 )
transform.eulerAngles = new Vector3( 0, Mathf.Asin(CrossPlatformInputManager.GetAxis("Horizontal")) * (180/Mathf.PI), 0 );
if ( CrossPlatformInputManager.GetAxis("Vertical")< 0)
transform.eulerAngles = new Vector3( 0, 90 + (Mathf.Acos(CrossPlatformInputManager.GetAxis("Horizontal")) * (180/Mathf.PI)), 0 );
//moves cube forward
if (CrossPlatformInputManager.GetAxis ("Vertical") < 0 || CrossPlatformInputManager.GetAxis ("Horizontal") < 0 || CrossPlatformInputManager.GetAxis ("Vertical") > 0 || CrossPlatformInputManager.GetAxis ("Horizontal") > 0) {
transform.Translate (moveVec * Time.deltaTime * 15);
//Debug.Log(CrossPlatformInputManager.GetAxis("Vertical"));
}
if (isBoosting == true) {
Camera.main.transform.rotation = Quaternion.Euler(transform.eulerAngles);
}
}
}
Answer by FortisVenaliter · Jan 27, 2016 at 06:59 PM
Your moveVec is in world coordinates. You want to move the object in local coordinates.
So, instead of saying:
moveVec = new Vector3(0,0,1);
You probably want something more along the lines of:
moveVec = transform.forward * 1;
That didn't seem to work, after commenting out the movement code,` transform.Translate (moveVec Time.deltaTime 15);`, I realized the rotation is not correct after switching the camera, so I think it's something to do with that?
Answer by Allmightysmiter · Feb 02, 2016 at 05:30 PM
I found the solution, I had to take the objects eulerAngles.y when pressing the button and add that to the current eulerAngles, unless the total is greater than 360, then subtract 360 by the added numbers to normalize it. Here's the revised code. using UnityEngine; using System.Collections; using UnityStandardAssets.CrossPlatformInput;
public class Controller : MonoBehaviour {
public float boostMultiplier =2;
public float angle = 0;
public float angleTwo = 0;
public Vector3 changeRot = new Vector3 (0, 0, 0);
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
bool isBoosting = CrossPlatformInputManager.GetButton ("Boost");
Vector3 moveVec = Vector3.forward;
//This is where joystick rotates player
if (CrossPlatformInputManager.GetAxis ("Vertical") > 0) {
angle = Mathf.Asin (CrossPlatformInputManager.GetAxis ("Horizontal")) * (180 / Mathf.PI) + changeRot.y;
if (angle <360){
transform.eulerAngles = new Vector3 (0, angle, 0);
}
else{
angle = Mathf.Abs(360 - angle);
transform.eulerAngles = new Vector3 (0, angle, 0);
}
}
if (CrossPlatformInputManager.GetAxis ("Vertical") < 0) {
angleTwo = (90 + (Mathf.Acos (CrossPlatformInputManager.GetAxis ("Horizontal")) * (180 / Mathf.PI))) + changeRot.y;
if (angleTwo < 360){
transform.eulerAngles = new Vector3 (0, angleTwo, 0);
}
else{
angleTwo = Mathf.Abs(360 - angleTwo);
transform.eulerAngles = new Vector3 (0, angleTwo, 0);
}
}
//moves cube forward
if (CrossPlatformInputManager.GetAxis ("Vertical") < 0 || CrossPlatformInputManager.GetAxis ("Horizontal") < 0 || CrossPlatformInputManager.GetAxis ("Vertical") > 0 || CrossPlatformInputManager.GetAxis ("Horizontal") > 0) {
transform.Translate (moveVec * Time.deltaTime * 5);
}
if (isBoosting == true) {
Camera.main.transform.rotation = Quaternion.Euler(transform.eulerAngles);
changeRot = transform.eulerAngles;
}
}
}