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 …
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.