Home > Net >  How to pick up item in range of player in third person?
How to pick up item in range of player in third person?

Time:11-20

I am trying to make my player pick up an item when I press E and drop it when I press E again. I want the item to appear in the player's hand. The player should only be able to pick up the item within a certain distance. My current PickUP script is here and the destination is attached to the players hand.

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

public class PickUp : MonoBehaviour
{
   
    public int number = 1;
    public Transform theDest;
   



    public void Update()
    {

        if (Input.GetKeyUp(KeyCode.E) && (number % 2) == 1)
        {
            GetComponent<BoxCollider>().enabled = false;
            GetComponent<Rigidbody>().useGravity = false;
            GetComponent<Rigidbody>().isKinematic = true;
            this.transform.position = theDest.position;
            this.transform.parent = GameObject.Find("Destination").transform;
            number = number   1;
        }
        else if (Input.GetKeyUp(KeyCode.E) && (number % 2) == 0)
        {
            GetComponent<Rigidbody>().isKinematic = false;
            GetComponent<BoxCollider>().enabled = true;
            this.transform.parent = null;
            GetComponent<Rigidbody>().useGravity = true;
            number = number - 1;
        }
    }


}

I did some research but I am confused and having trouble figuring out how to pick up the item at a certain distance. I followed the script from a tutorial but they were doing a first-person game and used "raycast". If someone can please help me figure out how to pick up the item within a certain distance and can maybe even help me figure out another way to pick up the item in 3rd person I would appreciate it. I tried to clarify as much as I could but let me know if you need more information. I am new here and don't exactly know what to include. (I am using the Unity Invector-3rd Person controller Lite if that helps)

CodePudding user response:

You can make a Circular player centric box collider with a certain radius and use Oncollisionenter and OnCollisionExit to know if the game object is next to the player

  • Related