hold_items

using UnityEngine; 
using System.Collections;

public class HoldItems : 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()     
  {         
    if (!ball)             
      return;          
    //We set the object parent to our guide empty object.         
    ball.transform.SetParent(guide);          
    //Set gravity to false while holding it         
    ball.GetComponent<Rigidbody>().useGravity = false;          
    //we apply the same rotation our main object (Camera) has.        
    ball.transform.localRotation = transform.rotation;        
    //We re-position the ball on our guide object          
    ball.transform.position = guide.position;          
    canHold = false;     
  }      
  
  private void throw_drop()     
  {         
    if (!ball)             
      return;          
    
    //Set our Gravity to true again.         
    ball.GetComponent<Rigidbody>().useGravity = true;          
    // we don't have anything to do with our ball field anymore          
    ball = null;          
    //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 

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source