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 /
avatar image
0
Question by tasti man LH · Aug 30, 2015 at 11:55 AM · transform.positiontriggerstags

Grabing Objects - Need Help

So I'm trying to have my player character grab an object.

The way how it works is that I have a trigger volume, called "Grabber", scripted to follow the player's x and y positions (I chose specifically to not parent it due to wanting full control over the Grabber in case if something went wrong) and whenever the player moves right and left the Grabber will change position to face the direction that the player is facing. Then the player presses Fire2 (the right click button) to pick up the object. The script then is looking for a GameObject with the "Object" tag, via GameObject.FindWithTag.

What's supposed to happen is the object's position should change to match the Grabber's position (meaning that in theory the object should be centered inside the Grabber). However, in the code as is, nothing's happening.

The only thing I can think of is that the formatting of the code and where it's placed is incorrect, otherwise I feel like the code should be correct...

 var grabberCube : GameObject;
 var player : GameObject; 
 var grabberPos : Vector3;  
 var grabberXPos : float; 
 var grabberXAdj : float = 0.0; 
 var grabberYPos : float;
 var grabberZPos : float;  
 var grabberLock : boolean = false; 
 var object : GameObject;
 var objectPos : Vector3; 
 var objectPosX : float;
 var objectPosY : float; 
 var objectPosZ : float;
 var otherObjPos : Vector3;
 
 function Start () {
 
 }
 
 function Update () {
     grabberPos = grabberCube.transform.position; 
     grabberXPos = grabberCube.transform.position.x; 
     grabberYPos = grabberCube.transform.position.y; 
     grabberZPos = grabberCube.transform.position.z; 
     grabberCube.transform.position.x = player.transform.position.x + grabberXAdj;
     grabberCube.transform.position.y = player.transform.position.y; 
     
     object = GameObject.FindWithTag("Object");
     objectPos = object.transform.position;
     objectPosX = object.transform.position.x; 
     objectPosY = object.transform.position.y; 
     objectPosZ = object.transform.position.z;
     
     if(cameraMovement.moveRight == true){ 
         grabberXAdj = 1.3;
     } 
     
     if(cameraMovement.moveLeft == true){ 
         grabberXAdj = -1.3;
     }
     
     if(Input.GetButton("Fire2") && cameraMovement.objectEnter == true){ 
         grabCheck();
         grabberLock = true;
     } 
     
     if(Input.GetButton("Fire2") && cameraMovement.objectEnter != true){
         grabberLock = true;
         print("Nothing to grab");
     }
     
     if(grabberLock == true && Input.GetAxis("Horizontal")<0){
         grabberXAdj = 1.3;
         //print("Right locked in");
     }
     
     if(grabberLock == true && Input.GetAxis("Horizontal")>0){
         grabberXAdj = -1.3;
         //print("Left locked in");
     }
 } 
 
 function grabCheck(){ 
     /*object = GameObject.FindWithTag("Object");
     objectPos = object.transform.position;
     objectPosX = object.transform.position.x; 
     objectPosY = object.transform.position.y; 
     objectPosZ = object.transform.position.z;*/ 
     objectPosX = grabberCube.transform.position.x; 
     objectPosY = grabberCube.transform.position.y; 
     objectPosZ = grabberCube.transform.position.z; 
     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

2 People are following this question.

avatar image avatar image

Related Questions

Need help with setting all items of a specific tag to "inactive" when i die. 1 Answer

Unity 2017 Not finding prefab tag 1 Answer

Change position of an object on a keystroke within a trigger only? 1 Answer

I don't can't make this car elevator work 1 Answer

How to change this.gameObject.tag? 2 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