- Home /
How to pick / drop object
How to pick and drop the object in the first person shooter .once we picked up the object should come along with the user can any one help me out with this using javascript ...
Answer by nastasache · Apr 30, 2014 at 02:09 PM
I have no javascript version, but you can use something very simple like this in C#:
TakeMe.cs:
using UnityEngine;
using System.Collections;
public class TakeMe : MonoBehaviour {
private Transform taker;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown("t")) {
transform.parent = taker;
} else if(Input.GetKeyDown("g")) {
transform.parent = null;
}
}
void OnTriggerEnter (Collider col) {
if(col.CompareTag("Player")) {
taker = col.transform;
}
}
void OnTriggerExit (Collider col) {
if(col.CompareTag("Player")) {
taker = null;
}
}
}
Supposed you have the FPS player tagged as "Player".
Attach the script to object you want to take (the object need to have a collider).
Then press t/g to pick-up/drop the object.
Note the pick-able object have to be placed in the root hierarchy of the scene; you have to improve the script for objects in other hierarchy levels.
tq its really help!!!!!!!!!!!!!!!!!!! tq so much. but i have a question, how to i drop to a specific place?
Your answer
Follow this Question
Related Questions
Picking up a box 2 Answers
Buff System 1 Answer
Can someone help me with the initialize and pick up script, please? 1 Answer
Weapon Pickup and change 2 Answers
Item pickup and drop function 2 Answers