- Home /
issue with player rotation
I was working on a player controller script for twin controller stick style movement. I was able to make it so that the play faces the direction they are moving if the right stick has no input and was able to make it so that the player faces the direction the right stick is pushed in. My issue is that when there is no input from either stick the player is forced up into a 0 rad position despite have a variable which is supposed to store the last angle the player was facing and set the rotation to that angle should there be no input from either stick. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player_Controller_New : MonoBehaviour {
public float speed;
public Fire_Arm weapon;
public float fireDelay;
public GameObject Ammo;
static public float lastAngle;
// Update is called once per frame
void Update () {
float x = Input.GetAxis ("Horizontal");
float y = Input.GetAxis ("Vertical");
transform.Translate (x * Time.deltaTime * speed, 0, y * Time.deltaTime * speed, Space.World);
float rx = Input.GetAxis ("Right_Horizontal");
float ry = Input.GetAxis ("Right_Vertical");
float angle = Mathf.Atan2 (rx, ry);
float altAngle = Mathf.Atan2 (x, y);
//In the below section when none of the sticks are in use it defaults character position to up, need to fix 6/17/2017
if (rx == 0 && ry == 0) { //This makes the player (in theory) look in the direction they are moving if there is no input from right stick
transform.rotation = Quaternion.EulerAngles (0, altAngle, 0);
lastAngle = altAngle;
} else if (ry == 0 && rx == 0 && x == 0 && y == 0) {
transform.rotation = Quaternion.EulerAngles (0, lastAngle, 0);
} else {
transform.rotation = Quaternion.EulerAngles (0, angle, 0);
lastAngle = angle;
}
}
}
Answer by Bill9009 · Jun 23, 2017 at 02:15 AM
Try to give it a range instead of checking if it is 0. Ex:
if ((rx < 0.1) && (rx > -0.1) && (ry < 0.1) && (ry > -0.1))
This might be happening because the Input.GetAxis() is returning 0.0001 instead of exactly zero.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Will someone please help me??? 2 Answers
How to make an object move to another object's location in a 2d game? 2 Answers