Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by tasti man LH · Aug 26, 2015 at 06:14 PM · inputtransform.positiontags

Grabbing Object Trouble

So what I’m trying to do right now is have my player character grab and push/pull objects around (think the crate pushing found in some platformer games).

The idea being that when Fire2 (right mouse button) is pressed, it will pick up the object.

I’ve got a trigger volume called “Grabber” that’s following the player character’s X and Y positions (I deliberately chose not to parent Grabber directly to the player character and instead via scripting because this gives me greater control over the Grabber in case if something bad happens). The tag that the Grabber is specifically looking for is “Object”. It’s set up so that if the object with the correct tag enters the Grabber, it will return the statement in a different script, ObjectEnter, as True. If that conditional is fulfilled in addition to Fire2 being pressed, it should then activate the “grabbing” function.

What’s supposed to happen is that after the Object is found via the GameObject.FindTag statement, and then the Object should change position to be identical to Grabber’s position (meaning that ideally, it should be held in the center of the Grabber). Unfortunately, that hasn’t happened.

It could be because I’m initializing the variables in the wrong place (as I was previously getting thrown errors when I did try to initialize them in what I thought was the normal place: at the top of the script before the Start function), or there could be an extra step I’m missing.

     var grabberCube : GameObject;
     var player : GameObject; 
     var grabberPos : Vector3;  
     var grabberXPos : float; 
     var grabberXAdj : float = 0.0; 
     var object : GameObject;
     var objectPos : Vector3; 
     var otherObjPos : Vector3;
     
     function Start () {
     
     }
     
     function Update () {
         grabberPos = grabberCube.transform.position; 
         grabberXPos = grabberCube.transform.position.x; 
         grabberCube.transform.position.x = player.transform.position.x + grabberXAdj;
         grabberCube.transform.position.y = player.transform.position.y; 
         //objectPos = object.transform.position; 
         //print(objectPos);
         
         
         if(cameraMovement.moveRight == true){ 
             grabberXAdj = 1.3;
         } 
         
         if(cameraMovement.moveLeft == true){ 
             grabberXAdj = -1.3;
         }
         
         if(Input.GetButton("Fire2") && cameraMovement.objectEnter == true){
             grabbing();
         } 
         
         if(Input.GetButton("Fire2") && cameraMovement.objectEnter != true){
             print("Nothing to grab");
         }
     } 
     
     function grabbing(){
         object = GameObject.FindWithTag("Object");
         objectPos = object.transform.position; 
         objectPos = grabberPos;
         print(objectPos);
         print("Grabbed!");
     }

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

25 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

Related Questions

2d touch not working 0 Answers

How Do I Destroy All Objects At a Given Location? 2 Answers

Setting transform.position of the Oculus Player Controller causes it to teleport away 1 Answer

How to fix (NullReferenceException: Object reference not set to an instance of an object) in my case 0 Answers

Do touch on point where ray hits 0 Answers


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