Hello there,
I have been working on a 2D game for a while now and I have recently come across a widely discussed “bug?” where OnTriggerExit/Stay/Enter do not work correctly when localscale is used.
OnTriggerEnter would get called the same way OnTriggerStay did and OnTriggerExit did not get called at all.
All the code below does is sending name of the object the player has collided with to the OnGUI in a different script if the action button (E) is pressed.
using UnityEngine;
using System.Collections;
public class Buildings : MonoBehaviour {
public static bool PlayerInRange=false;
public static bool ActionButtonPressed=false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerStay2D(Collider2D other) {
IsActionButtonPressed ();
}
void OnTriggerExit2D(Collider2D other)
{
Debug.Log ("Bye");
ActionButtonPressed = false;
}
void IsActionButtonPressed()
{
if(Input.GetKeyDown (KeyCode.E))
{
ActionButtonPressed=true;
UI.MenuToDisplay = gameObject.name;
}
}
}
This is the code responsible for rotating player’s character. With the localscale shown below, the above code did not work. As soon as 3 lines(6 in total, 3 for each direction) were taken out, OnTrigger worked just fine.
public void LeftRightMovement () //Left/Right movement method
{
AnimMovement.SetFloat ("Speed", Mathf.Abs (Input.GetAxis (axisNameMovement)));
if (Input.GetAxis (axisNameMovement) < 0) {
Vector3 newScale = transform.localScale; //Removed this
newScale.x = -5.0f; //Removed this
transform.localScale = newScale; //Removed this
CurrentFuel -= WalkFuelUsage; //subtract the fuel usage for walking from the CurrentFuel
setDirection ("Left");
} else if (Input.GetAxis (axisNameMovement) > 0) {
Vector3 newScale = transform.localScale; //Removed this
newScale.x = 5.0f; //Removed this
transform.localScale = newScale; //Removed this
CurrentFuel -= WalkFuelUsage; //subtract the fuel usage for walking from the CurrentFuel
setDirection("Right");
}
transform.position += transform.right * Input.GetAxis (axisNameMovement) * speed * Time.deltaTime;
}
I am asking if anyone can think of an alternative to localscale. All I want is rotate player’s sprite around to correspond to the approperiate direction (left/right). Thanks in advance