- Home /
pick up/throw object
the script allows me to pick up the ball, but it allows me to do this no matter where on the map i am, and i cannot put the ball down,
throw the ball, either, which falls down under the 'dropping'
to pick up the ball i need to freeze its position or it will rotate out of my grasp and just fall
although the position is frozen the rotation is not, the block will pass through anything with a collider/ rigid body, only while picked up though, but it is cool that it acts similar to a beyblade. to clarify it hits the other thing, we'll say wall, and stops rotating but it will proceed to go into the wall if pushed a little
.
.
script:
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
public class Pickup_Throw : MonoBehaviour
{
public float speed = 10;
public bool canHold = true;
public GameObject ball;
public Transform guide;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (!canHold)
throw_drop();
else
Pickup();
}//mause If
if (!canHold && ball)
ball.transform.position = guide.position;
}//update
//We can use trigger or Collision
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "ball")
if (!ball) // if we don't have anything holding
ball = col.gameObject;
}
//We can use trigger or Collision
void OnTriggerExit(Collider col)
{
if (col.gameObject.tag == "ball")
{
if (canHold)
ball = null;
}
}
private void Pickup()
{
//ball.GetComponent<Rigidbody>().i = true;
//We set the object parent to our camera.
ball.transform.SetParent(this.gameObject.transform);
//assuming you forward is in z-axis, we add some distance to object so you can see it carrying it.
ball.transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z); // (4) is how far the ball will be from camera position.
}
private void throw_drop()
{
if (!ball)
{
return;
}
//Set our Gravity to true again.
ball.GetComponent<Rigidbody>().useGravity = true;
ball = null; // we don't have anything to do with our ball field anymore
//Apply velocity on throwing
guide.GetChild(0).gameObject.GetComponent<Rigidbody>().velocity = transform.forward * speed;
//Unparent our ball
guide.GetChild(0).parent = null;
canHold = true;
}
}//class
Answer by Tr_1P_l3 · Apr 05, 2021 at 12:44 AM
i spent about 7 hours between trying my own code and tutorials so first thank you very much cause your code is the only 1 worked but i have done only 1 change in the if statement (am trying to pick box with character without using the mouse buttons i.e.
am trying to move (grab) boxes in game scene like the technique in game (little nightmare) or like (Inside)) . the change i did
if (Input.GetKeyDown(KeyCode.E))
rest of the code is the same like yours.
when i try the code, it pick up just once and i have to drag the box from the hierarchy to the player inspector to be able to pick up.
A.T.M i want to know how to make box picked more than once and with any box with tag box cause once i drop the box, it do not collide with it anymore.
sorry for disturbing and writing too much :(
Your answer
Follow this Question
Related Questions
Pickup script error 1 Answer
Pickup item and move with it -1 Answers
i need my character to pick up objects and drop them.... 1 Answer
Can someone help me with the initialize and pick up script, please? 1 Answer
Pickup and move GameOjbect? 1 Answer