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
1
Question by SakuOlin · Jul 28, 2012 at 04:56 PM · javascriptobjectgrab

Checking Player's distance to an Object

Hello. I am using a script which lets me grab and carry Objects around.

But the problem is, I can grab it anywhere, and that is not what I want.

Could someone help me out and tell me how could I check the Player's distance to an Object, not allowing Player to grab the Object from too far. I apologize, English is not my first language.

Anyway, this is the code I am using.

private var pickObj: Transform = null; private var hit: RaycastHit; private var dist: float; private var newPos: Vector3;

function Update(){

 if (Input.GetMouseButton(0)){ // if left button creates a ray from the mouse
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     if (!pickObj){ // if nothing picked yet...
         if (Physics.Raycast(ray, hit) && hit.transform.tag == "Pick"){
             // if it's a rigidbody, zero its physics velocity
             if (hit.rigidbody) hit.rigidbody.velocity = Vector3.zero;
             pickObj = hit.transform; // now there's an object picked
             // remember its distance from the camera
             dist = Vector3.Distance(pickObj.position, Camera.main.transform.position);
         }
     }
     else { // if object already picked...
         newPos = ray.GetPoint(dist); // transport the object
         pickObj.position = newPos;   // to the mouse position 
     }    
 }
 else { // when button released free pickObj
     pickObj = null;
 }

}

Also, if you know how to prevent the Object going through walls, could you tell me how? Thanks in advance.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by thellama · Jul 28, 2012 at 06:21 PM

Notice I added a distance float and check between the object and the camera on your third if statement. Just adjust the distance float and it will check the distance and see if the object is within your grabbable distance!

 var pickObj: Transform = null;
 private var hit: RaycastHit;
 private var dist: float;
 private var newPos: Vector3;
 var distance : float = 5;
 
 function Update(){
 
     if (Input.GetMouseButton(0)){ // if left button creates a ray from the mouse
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (!pickObj){ // if nothing picked yet...
             if (Physics.Raycast(ray, hit) && hit.transform.tag == "Pick" && Vector3.Distance(Camera.main.transform.position, hit.transform.position) < distance){
                 // if it's a rigidbody, zero its physics velocity
                 if (hit.rigidbody) hit.rigidbody.velocity = Vector3.zero;
                 pickObj = hit.transform; // now there's an object picked
                 // remember its distance from the camera
                 dist = Vector3.Distance(pickObj.position, Camera.main.transform.position);
             }
         }
         else { // if object already picked...
             newPos = ray.GetPoint(dist); // transport the object
             hit.rigidbody.MovePosition(newPos);   // to the mouse position 
         }    
     }
     else { // when button released free pickObj
         pickObj = null;
     }
 }

Also note I changed

 pickObj.position = newPos;   // to the mouse position

To

 hit.rigidbody.MovePosition(newPos);

This is so it will use the rigidbody and collider to check it's position in space against other colliders. Not 100% accurate but it works.

Comment
Add comment · Show 1 · 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
avatar image Seth-Bergman · Jul 29, 2012 at 08:03 AM 0
Share

I believe Aldo's solution below should allow for that, by using parenting method the object retains its normal collision

avatar image
1

Answer by aldonaletto · Jul 28, 2012 at 08:47 PM

There's a new version of this script, and it uses a grabRange parameter to define the max distance. It also has pickPos, which sets the object position relative to the character. When an object inside grabRange is clicked, it's rigidbody.velocity is controlled in order to bring it to pickPos. Since the object must have a rigidbody, the script adds one to the object if there's none, removing it when the object is released:

var grabRange: float = 2; var pickPos: Vector3 = Vector3 (0, 0.5, 1.1); var force: float = 30;

private var pickObj: Transform = null; private var rBody: Rigidbody; private var hadRBody: boolean; private var freezeRot: boolean;

function Update(){

 if (Input.GetMouseButtonDown(0)){ // if left button pressed...
     var hit: RaycastHit;
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition); // create a ray from the mouse pointer
     // if object &quot;Pick&quot; clicked, and inside grabRange...
     if (Physics.Raycast(ray, hit) && hit.transform.tag == "Pick" && hit.distance < grabRange){
         pickObj = hit.transform; // save the object's transform...
         pickObj.parent = transform; // child it to the player...
         rBody = pickObj.rigidbody; // and get its rigidbody, if any
         if (rBody){ // if already had a rigidbody... 
             freezeRot = rBody.freezeRotation; // save its freezeRotation property
             hadRBody = true;
         } else { // if no rigidbody, add one:
             rBody = pickObj.gameObject.AddComponent(Rigidbody);
             hadRBody = false;
         }
         rBody.freezeRotation =true; // disable physical rotations
     }
 }
 if (Input.GetMouseButtonUp(0) && pickObj){
     if (hadRBody){ // if already had a rigidbody...
         rBody.freezeRotation = freezeRot; // restore freezeRotation
     } else { // if not, destroy the added rigidbody:
         Destroy(rBody);
     }
     pickObj.parent = null; // unchild the object...
     pickObj = null; // and signal that's nothing is picked anymore
 }

}

function FixedUpdate(){ if (pickObj){ var error = transform.TransformPoint(pickPos) - pickObj.position; rBody.velocity = force error; } } EDITED:* There was some variable declarations missing at the top - answer fixed (again...)

Comment
Add comment · Show 6 · 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
avatar image SakuOlin · Jul 29, 2012 at 10:37 AM 0
Share

No matter what I try, I still get loads of errors. I tried adding this to the original and using this itself, but neither of those ways worked. I tried adding variables but I still got errors. If you can help me, thanks in advance.

avatar image Seth-Bergman · Jul 29, 2012 at 10:46 AM 0
Share

add these lines at the top:

private var pickObj: Transform;

private var rBody : Rigidbody;

var grabRange = 2.0;

var pickPos : Vector3;

if there are still errors after that, what are they?

avatar image SakuOlin · Jul 30, 2012 at 07:51 AM 0
Share

$$anonymous$$ Identifier: 'force'

I have no idea what should I do. Any help?

avatar image Seth-Bergman · Jul 30, 2012 at 10:16 AM 1
Share

hmm, not sure what the value should be.. but add this line at the top:

var force = 1.0;

not entirely sure what that part does..

avatar image aldonaletto · Jul 31, 2012 at 12:41 AM 0
Share

Dear God! Due to some mysterious reason, the first lines of this script disappeared! It's fixed now (@Seth Bergman almost guessed the missing lines...)

Show more comments

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

9 People are following this question.

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

Related Questions

Adding an object to an array of custom objects (JS) 1 Answer

Shutting a function when another function calling. 1 Answer

Drawing a 3D object javascipt 0 Answers

Mecanim not updating script when it switches state? 2 Answers

Disable GameObject Only Father Not Children 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