- Home /
Triggers and Controls not working after player turns in endless runner
I am new at unity and I am making endless runner type game, but with end, so it could have levels with different enviroment. Everythink seemed to work ok until I added a trigger that makes player rotate on road turn. Before turn everythink is alright and after it turns, player keeps running forward, but same controls and triggers don't work anymore. I am inserting whole player script, because I don't have any idea why it doesn't work.
public class PlayerControler : MonoBehaviour{
public bool jump = false;
public bool slide = false;
public bool right = false;
public bool left = false;
public bool die = false;
public bool stop = false;
public bool win = false;
public Animator anim;
public Vector3 collidercenter;
public GameObject player;
private Touch initialTouch = new Touch();
private float distance = 0;
private bool hasSwiped = false;
private bool isGameStarted;
public bool death = false;
public Image gameOverImg;
public CapsuleCollider myCollider;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
myCollider = GetComponent<CapsuleCollider>();
collidercenter = myCollider.center;
}
// Update is called once per frame
void FixedUpdate()
{
foreach (Touch t in Input.touches)
{
if (t.phase == TouchPhase.Began)
{
initialTouch = t;
}
else if (t.phase == TouchPhase.Moved && !hasSwiped)
{
float deltaX = initialTouch.position.x - t.position.x;
float deltaY = initialTouch.position.y - t.position.y;
distance = Mathf.Sqrt((deltaX * deltaX) + (deltaY * deltaY));
bool swipedSideWay = Mathf.Abs(deltaX) > Mathf.Abs(deltaY);
if (distance > 100f)
{
if (swipedSideWay && deltaX > 0)
{
left = true;
}
if (swipedSideWay && deltaX <= 0)
{
right = true;
}
if (!swipedSideWay && deltaY > 0)
{
slide = true;
StartCoroutine(SlideController());
}
if (!swipedSideWay && deltaY <= 0)
{
jump = true;
StartCoroutine(JumpController());
}
hasSwiped = true;
}
}
else if (t.phase == TouchPhase.Ended)
{
initialTouch = new Touch();
hasSwiped = false;
}
}
transform.Translate(0, 0, 0.1f);
if (Input.GetKey(KeyCode.Space) && stop == false && slide == false && right == false && left == false)
{
jump = true;
StartCoroutine(JumpController());
}
if (Input.GetKey(KeyCode.DownArrow) && stop == false && jump == false && right == false && left == false)
{
slide = true;
StartCoroutine(SlideController());
}
if (Input.GetKey(KeyCode.LeftArrow) && stop == false && right == false && jump == false && slide == false)
{
left = true;
transform.Translate(-0.9f, 0, 0.1f);
}
if (Input.GetKey(KeyCode.RightArrow) && stop == false && jump == false && slide == false && left == false)
{
right = true;
transform.Translate(0.9f, 0, 0.1f);
}
if ((jump == true) && stop == false && slide == false && right == false && left == false)
{
anim.SetBool("Jump", true);
myCollider.center = myCollider.center + new Vector3(0, 4f, 2f);
transform.Translate(0, 0.15f, 0.2f);
}
else if (jump == false)
{
anim.SetBool("Jump", false);
}
myCollider.center = collidercenter;
if ((slide == true) && stop == false && jump == false && right == false && left == false)
{
anim.SetBool("Slide", slide);
transform.Translate(0, 0, 0.1f);
myCollider.center = Vector3.zero;
myCollider.height = 2.05f;
}
else if (slide == false)
{
anim.SetBool("Slide", slide);
myCollider.height = 6.5f;
}
}
IEnumerator JumpController()
{
jump = true;
yield return new WaitForSeconds(0.5f);
jump = false;
}
IEnumerator SlideController()
{
slide = true;
yield return new WaitForSeconds(0.7f);
slide = false;
}
IEnumerator WinAnim()
{
anim.SetBool("win", true);
win = true;
yield return new WaitForSeconds(0.5f);
anim.SetBool("win", false);
win = false;
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "obstacle")
{
Destroy(player, 2.5f);
anim.SetBool("die", true);
StartCoroutine(WaitForSceneLoad());
}
if (other.gameObject.tag == "WinPoint")
{
Destroy(player, 15f);
StartCoroutine(WinAnim());
if (win == false)
{
anim.SetBool("win02", true);
}
StartCoroutine(WaitForSceneToLoad());
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "stop")
{
jump = false;
slide = false;
left = false;
right = false;
stop = true;
}
if (other.gameObject.tag == "rotate")
{
GetComponent<Rigidbody>().angularVelocity = new Vector3(0, -2, 0);
StartCoroutine(StopRotation());
}
}
private IEnumerator WaitForSceneToLoad()
{
yield return new WaitForSeconds(3);
SceneManager.LoadScene("WinScene");
}
private IEnumerator WaitForSceneLoad ()
{
yield return new WaitForSeconds(5);
SceneManager.LoadScene("GOver");
}
private IEnumerator StopRotation()
{
yield return new WaitForSeconds(0.8f);
GetComponent<Rigidbody>().angularVelocity = new Vector3(0, 0, 0);
GetComponent<Transform>().eulerAngles = new Vector3(0, -90, 0);
}
}
Do you have any idea what is wrong, why it is not working properly and how to fix it?
Thank you very much
Answer by xxmariofer · Mar 23, 2020 at 09:54 AM
because when moving the object you are using transform.translate using world axis, what that means is that if you are moving right for example, and you do
transform.Translate(0, 0.15f, 0.2f);
you are just doing an step forward in that direction. if you have rotated to the right, you should start moving using other axis like
transform.Translate(0.15f, 0, 0.2f);
i feel it is imposible to implement rotation in your game unless you refactor your whole code, you should be moving using the
transform.forward * aCostantAmount
hardcoding all the variables will make the code unfixable in the long run