Search code examples
unity-game-engine2d

Unity weapon equip issues


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControllerV2 : MonoBehaviour
{
    public float angle;
    public bool WeaponPickUp;
    public float curShotDelay;
    public bool verIdle = false;
    public float movementSpeed = 3.0f;
    SpriteRenderer rend;
    Vector2 movement = new Vector2();
    Rigidbody2D rigidbody2D;
    [SerializeField] float normalSpeed, runSpeed;

    Animator animator;

    public GameObject weapon;
    public GameObject sPoint;
    public GameObject AWP;

    public bool isPickUp;

    GameObject nearObject;
    // Start is called before the first frame update
    void Start()
    {
        rend = GetComponent<SpriteRenderer>();
        animator = GetComponent<Animator>();
        rigidbody2D= GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        PickUp();
        UpdateState();
        Reload();
    }
    void Direction()
    {
        Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition + Vector3.forward * 10f);

        float angle = Mathf.Atan2(
            this.transform.position.y - mouseWorldPosition.y,
            this.transform.position.x - mouseWorldPosition.x) * Mathf.Rad2Deg;
    }

    private void FixedUpdate()
    {
        MoveCharacter();
        RotateToMouseDir();
    }

    private void MoveCharacter()
    {
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");

        

        rigidbody2D.velocity = movement.normalized * movementSpeed;
        Vector3 moveDir = (Vector3.forward * movement.y) + (Vector3.right * movement.x);
    }

    void RotateToMouseDir()
    {
        Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition + Vector3.forward * 10f);

        angle = Mathf.Atan2(
            this.transform.position.y - mouseWorldPosition.y,
            this.transform.position.x - mouseWorldPosition.x) * Mathf.Rad2Deg;

        if (angle > -90 && angle < 90)
        {
            rend.flipX = true;
        }
        if ((angle > -180 && angle < -90) || (angle > 90 && angle < 180))
        {
            rend.flipX = false;
        }
        // Debug.Log(angle);
    }

    private void UpdateState()
    {
        if (Mathf.Approximately(movement.x, 0) && Mathf.Approximately(movement.y, 0))
        {
            animator.SetBool("IsMove", false);
            animator.SetBool("IsIdle", true);
        }
        else
        {
            animator.SetBool("IsMove", true);
            animator.SetBool("IsIdle", false);
        }

        if (Input.GetKey(KeyCode.LeftShift))
        {
            movementSpeed = runSpeed;
            animator.SetBool("IsRun", true);
        }
        else
        {
            movementSpeed = normalSpeed;
            animator.SetBool("IsRun", false);
        }

        if (movement.x > 0)
        {
            animator.SetFloat("horIdle", 1);
        }
        else if (movement.x < 0)
        {
            animator.SetFloat("horIdle", -1);
        }

        if (movement.y > 0)
        {
            verIdle = true;
        }
        else if (movement.y < 0)
        {
            verIdle = false;
        }

        animator.SetFloat("horizontal", movement.x);
        animator.SetFloat("vertical", movement.y);
    }
    void Reload()
    {
        curShotDelay += Time.deltaTime;
        // Debug.Log(curShotDelay);
    }

    void PickUp()
    {
        if (isPickUp = true && Input.GetKeyDown(KeyCode.E))
        {
        GameObject weapon = Instantiate(AWP) as GameObject;
        sPoint.transform.SetParent(weapon.transform, false);
        }
    }

    void OnTriggerEnter2D(Collider2D col)
    {
        if (col.tag == "Weapon")
        {
            isPickUp = true;
        }

        if (col.tag == "Weapon")
        {
            Debug.Log("Touched Weapon");
            if (Input.GetKeyDown(KeyCode.E))
                WeaponPickUp = true;
        }
    }

    void OnTriggerExit2D(Collider2D col)
    {
        if (col.tag == "Weapon")
        {
            isPickUp = false;
        }
    }
}

This is my code and i managed to spawn the weapon when I interacted with the item, but couldn't find a way to make it spawn on my character and neither to follow my character.

I made a spawn point inside the character prefab, but it seems like it spawns at the initial location of the spawn point and it doesn't move.


Solution

  • You're parenting the object the wrong way around on line 141. Currently you are taking the sPoint and resigning it from your player to the instantiated weapon.

    weapon.transform.SetParent(sPoint.transform, false);