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 whitewolfwhale · May 19, 2015 at 10:08 AM · physicscollidertrigger

I need help making a trigger make an a game object so that it can be manipulated

Hello all, I am very new to scripting and am working on a project where a third person character is able to run around and pick up objects in the world and toss them. I had a previous script working that detected objects to be picked up via raycasting, but had a really hard time getting it to feel right since you can free look and orbit with the camera. Originally, the raycast came from an object in front of the character.

I'm experimenting with a new method for picking up objects where there is a large trigger space in front of the main character so that the player can clearly see what's in the pickup zone. I can't figure out how to identify the gameobject that is in the trigger zone so that it can take on the pickupable class and become carried. I'd like for the player to only be able to hold one object at a time. The "pickupable" class is a component that is on certain types of objects. You can also see in my code where I'm setting it up as a tag I can apply to prefabs, in case that will work better.

I've never posted before and have been looking at stuff trying to fix this all day. Hopefully you folks can help me. I'm attaching my pickup code. Thanks in advance. enter code hereusing UnityEngine; using System.Collections;

public class betterPickUpObject : MonoBehaviour {

     GameObject grabpoint;
     GameObject holdpoint;
     bool carrying;
     bool held;
     bool goodforpickup;
     GameObject carriedObject;
     public float distance;
     public float smooth;
     public float speed;
     private float throwdirection;
     public float pullspeed;
     public GameObject MainCamera;
     private Vector3 SpawnPosition;
     private Vector3 dir;
     
     // Use this for initialization
     void Start() {
         
         grabpoint = GameObject.FindWithTag("grabpoint"); 
         holdpoint = GameObject.FindWithTag ("holdpoint");
         MainCamera = GameObject.FindWithTag ("MainCamera");
 
     }
     
     
     // Update is called once per frame
     void Update () {
         
         if (carrying) {
             carry (carriedObject);
             
         } 
         else 
         {
             pickup();
             
         }
         if (held) {
             holding (carriedObject);
             
             checkThrow ();
         }
         else
         {
             pickup();
             
         }
     }
     
     void carry(GameObject o) {
         dir = holdpoint.transform.position - carriedObject.transform.position;
         dir = dir.normalized;
         carriedObject.rigidbody.AddForce (dir * pullspeed);
         o.rigidbody.isKinematic = false;
     }
     
     void OnTriggerEnter(Collider other){
         
     carriedObject.transform.position = Vector3.Lerp (holdpoint.transform.position, holdpoint.transform.position, Time.deltaTime * smooth);
     Debug.Log ("Object in Trigger");
     {
         if
             (other.gameObject == carriedObject) {
             held = true;
                 
                 
         }
     }
 }

     
     void holding (GameObject o){
         carriedObject.transform.position = Vector3.Lerp (holdpoint.transform.position, holdpoint.transform.position, Time.deltaTime * smooth);
         o.transform.Rotate (new Vector3 (15, 15, 0) * Time.deltaTime);
     }
     
     void OnTriggerEnter (Collision hit) {
     if (hit.gameObject.tag == "Pickupable") {
         Debug.Log ("Valid Object in Collider Box");
         hit.gameObject = 
         goodforpickup = true; 
     }
 }

     void OnTriggerExit (Collision hit) {
         if (hit.gameObject.tag == "Pickupable") {
             Debug.Log ("Valid Object left Collider Box");
             goodforpickup = false; 
                             }
 }

     void pickup (){
 
         if(Input.GetButtonDown("Fire1")) {
         //int x = Screen.width / 2;
         //int y = Screen.height /2;
         if (goodforpickup = true){
         
         //Ray ray = MainCamera.camera.ScreenPointToRay(new Vector3 (x,y));
         //RaycastHit hit;
         
         //if(Physics.Raycast(ray, out hit)){
             Pickupable p = hit.GameObject.GetComponent<Pickupable>();
         //Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
         //Debug.DrawRay(transform.position, forward, Color.blue);
         if(p != null){
             carrying = true;
             carriedObject = p.gameObject;
 }
             }
     }
     }

         

     //void pickup(){
         
         //if (Input.GetButtonDown("Fire1")) {
             //int x = Screen.width / 2;
             //int y = Screen.height /2;
         //while (goodforpickup = true){
             
             //Ray ray = MainCamera.camera.ScreenPointToRay(new Vector3 (x,y));
                 //RaycastHit hit;
             
             //if(Physics.Raycast(ray, out hit)){
                 //Pickupable p = hit.collider.GetComponent<Pickupable>();
                 //Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
                 //Debug.DrawRay(transform.position, forward, Color.blue);
                 //if(p != null){
                     //carrying = true;
                     //carriedObject = p.gameObject;
                     
     void checkThrow (){
         if (Input.GetButtonDown ("Fire1")) {
             
             throwObject ();
         }
     }
     void throwObject (){
         carrying = false;
         held = false;
         carriedObject.gameObject.rigidbody.isKinematic = false;
         
         //throwdirection = (MainCamera.transform.forward);
         
         carriedObject.gameObject.rigidbody.AddTorque (Camera.main.transform.forward * speed);
         carriedObject.gameObject.rigidbody.AddForce (Camera.main.transform.forward * speed);
         
         //carriedObject.gameObject.rigidbody.AddForce 
         //(throwdirection * speed, 0, 0);
         
         //carriedObject.gameObject.rigidbody.transform.TransformDirection (new Vector3 (speed, 0, 0));
         
         
         carriedObject = null;
         
         
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Collider/trigger collision causing physics glitch 0 Answers

overlapsphere to destroy NPCs on exit 1 Answer

Collider Lifts me off Ground ??? And I'm afraid of heights !!! 1 Answer

Prevent shooting when gun is inside wall 1 Answer

How do i get OnTriggerStay2D to work? 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