Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by hen0029 · Jun 18, 2020 at 12:55 PM · pickupdropthrow

pick up/throw object

  1. 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,

  2. throw the ball, either, which falls down under the 'dropping'

  3. to pick up the ball i need to freeze its position or it will rotate out of my grasp and just fall

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 :(

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

128 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges