- Home /
Picking up an Object and locking its position relative to parent.
Im trying to make so a clickable object in the game can be clicked on, ideally the object would smoothly travel to just bellow and in front of the camera and stay there, instead it floats around slightly unpredictably, somewhat like when you pick up a cabbage in skyrim. I couldnt get lerp to work, in fact trying to define the position at all resulted in it appearing behind the camera far from the player. Also when space is pressed to drop it, it still seems to want to follow the camera loosely, just with gravity on.
using UnityEngine;
using System.Collections;
public class Pickup : MonoBehaviour
{
private GameObject player;
private Crosshair looking;
private bool pickedup;
void Awake()
{
player = GameObject.Find("Player/Camera");
looking = player.GetComponent<Crosshair>();
}
void OnMouseDown()
{
if(looking.imnear)
{
if(pickedup == false)
{
transform.parent = player.transform;
GetComponent<Rigidbody>().mass = 0.1f;
GetComponent<Rigidbody>().useGravity = false;
//line for objects new position relative to the camera
pickedup = true;
Debug.Log ("Picked Up");
}
}
}
void OnMouseDrag()
{
if(pickedup == true)
{
audio.Play();
//maybe change colour of halo
}
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
if(pickedup == true)
{
//drop command unchilds from camera
GetComponent<Rigidbody>().mass = 1f;
GetComponent<Rigidbody>().useGravity = true;
pickedup = false;
Debug.Log ("Dropped");
}
}
}
}
Just realised I havent put in the line to unchild thepickup object on the drop command, thats why its not dropping properly.
Answer by henrypuspurs · Jul 26, 2013 at 09:10 PM
Found the answer, thanks to some help from a guy at my uni. In this code the public Vector3 "puloc" (picked up location) will define where the object this script is on will end up when clicked on it.
using UnityEngine;
using System.Collections;
public class Pickup : MonoBehaviour
{
private GameObject player;
private Crosshair looking;
private bool pickedup;
public Vector3 puloc;
void Awake()
{
player = GameObject.Find("Player/Camera");
looking = player.GetComponent<Crosshair>();
}
void OnMouseDown()
{
if(pickedup && Input.GetKeyDown(KeyCode.Mouse0) && audio.isPlaying == false)
{
audio.Play();
}
else if(looking.imnear && Input.GetKeyDown(KeyCode.Mouse0) && pickedup == false)
{
transform.parent = player.transform;
transform.localPosition = puloc;
GetComponent<Rigidbody>().isKinematic = true;
pickedup = true;
Debug.Log ("Picked Up");
}
}
void OnMouseUp()
{
if(Input.GetKeyUp(KeyCode.Mouse0) && audio.isPlaying)
{
audio.Stop();
}
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Mouse1) && pickedup == true)
{
transform.parent = null;
GetComponent<Rigidbody>().isKinematic = false;
pickedup = false;
Debug.Log ("Dropped");
}
}
}
Your answer
Follow this Question
Related Questions
trying to make an object move up and stop via trigger 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Move Objects arranged in Circle Forward 1 Answer
Audio Play Once 2 Answers
Problem with C#. 1 Answer