Hello.
I’m trying to make 2D platformer run n shoot style game with Unity 5.1, but I’m stuck already ![]()
When I’m pointing gun at right it works perfectly but when I move mouse to left side of player, the player flips as it should, and gun flashes on both right and left side of player like crazy. On right gun points to exact opposite direction of mouse, as if it’d try to point on both sides.
Another bug when moving mouse back and forth → mouse is on right side, but gun is on left side pointing exact opposite direction of mouse. This is ‘fixed’ by moving mouse back and forth again.
Gun is childed under empty GameObject, which has this script attached:
using UnityEngine;
using System.Collections;
public class Gun : MonoBehaviour {
Quaternion rotation;
public Transform shoulder; //empty GameObject to place gun
public Transform target; //this is mouse, works fine
public Transform flipper; //empty GameObject to check if sprite should flip
public bool DirectionRight = true;
void Update ()
{
transform.position = shoulder.position;
if (target.position.x > flipper.position.x && DirectionRight == false) Flip();
if (target.position.x < flipper.position.x && DirectionRight == true) Flip();
if (DirectionRight == true && target.position.x > flipper.position.x)
{
rotation = Quaternion.LookRotation (target.transform.position - transform.position, transform.TransformDirection (Vector2.up));
transform.rotation = new Quaternion (0, 0, rotation.z, rotation.w);
}
if (DirectionRight == false && target.position.x < flipper.position.x)
{
rotation = Quaternion.LookRotation (target.transform.position - transform.position, transform.TransformDirection (Vector2.down));
transform.rotation = new Quaternion (0, 0, rotation.z, rotation.w);
}
}
void Flip()
{
DirectionRight = !DirectionRight;
Vector2 tempScale = transform.localScale;
tempScale.x *= -1;
transform.localScale = tempScale;
}
}