Question by
idab1 · Sep 06, 2017 at 05:51 PM ·
2d-platformerif-statementsflipping
Problem with if's conditions
I was making 2d platformer where arm was rotating with mouse and I wanted to flip player in X axis when arm's rotation value is above 0 and less than 180 so i wrote this script:`using System.Collections; using System.Collections.Generic; using UnityEngine;
public class KnightTurning : MonoBehaviour {
public GameObject Arm;
public GameObject Knight;
private bool haveFlipped = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 diffrence = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
diffrence.Normalize();
float RotationZ = Mathf.Atan2(diffrence.y, diffrence.x) * Mathf.Rad2Deg;
if(Arm.transform.rotation.z > 0 && Arm.transform.rotation.z < 180 && haveFlipped == false)
{
Vector3 position = new Vector3(0.125f, 0f, 0f);
Arm.transform.localPosition += position;
Flip();
}
else if (Arm.transform.rotation.z <= 0 && Arm.transform.rotation.z > -180 && haveFlipped == true)
{
Vector3 position = new Vector3(-0.125f, 0f, 0f);
Arm.transform.localPosition += position;
haveFlipped = false;
Knight.transform.localScale = new Vector3(1f, 1f, 1f);
}
}
void Flip()
{
Knight.transform.localScale = new Vector3(-1f, 1f, 1f);
haveFlipped = true;
}
} ` Issue with taht script is that arm's rotation alue can be less than 0 and still doesn't flip. Can you tell me what am I doing wrong?
Comment
Your answer
Follow this Question
Related Questions
localScale.x Affecting Player Translations 0 Answers
Change starting position of Instantiate object 1 Answer
Flipping sprite resets on no input 1 Answer
transform.localScal not flipping 0 Answers