Question by
Ashton-Benedict · Jul 11, 2016 at 08:51 PM ·
c#scripting problem2d-platformergunequip
2D Platformer Gun Equip Script Not Working
I have a 2D Platformer game and I have weapons on the ground that I want to be able to pick up. And so I made this script trying to do that and I'm trying to make the gun become a child of the player when it picks the gun up so that I can animate it and so it will follow the player.
Script attached to "Player"
public class Gun : MonoBehaviour {
public bool equipped = false;
private GunEquip equip;
Animator a;
void Start()
{
a = GetComponent<Animator>();
}
void OnCollisionEnter2D(Collider2D coll)
{
equip = GetComponent<GunEquip>();
if (coll.gameObject.tag=="Pickup" && Input.GetKeyDown(KeyCode.E))
{
equipped = true;
gameObject.GetComponent<GunEquip>().Equip();
}
}
void Update()
{
if(equipped==true)
{
a.SetBool("Equipped", true);
}
}
void FixedUpdate()
{
a.SetBool("Equipped", equipped);
}
}
Script attached to "Gun"
public class GunEquip : MonoBehaviour {
public void Equip()
{
transform.parent.tag = "Player";
}
}
Comment
Your answer
Follow this Question
Related Questions
Using multiple materials on a 2d mesh 0 Answers
Cannot delay player respawn. 2 Answers
Why doesn't this script work? 1 Answer
A* Pathfinding and keep the enmy at range 0 Answers
How to play animations from script. 0 Answers