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 derrtyones · May 02, 2014 at 01:31 PM · javascriptcollisiongameobjectrigidbodyvelocity

Dragging object out of position

I am really having a hard time with one of my scripts. Mainly because my coding experience stops here.

Imagine you have a desk with a drawer inside. In this case the desk is invisible for now. I have a basic "drag rigidbody" script so I can drag objects around. The drawer has fixed positions so it can only move into one direction (only Z, or X). Gravity is off as well.

So now with the dragging script you can drag the drawer out of the desk but it doesn't have a limit of where it can go. So I needed to write a script to stop this. This script simply teleports the drawer back into it's original position. NOTE: I have tried placing invisible objects as colliders but you're still able to drag them through.

With my script below you would maybe say: simple, don't teleport the drawer back! I tried, it stops at the limit, but you can still drag it further out (even with the velocity at 0).

So what am I trying to achieve?

I want to make it possible to drag the drawer out of the desk, but with limits. Take a look at my beautiful picture below. In this case we only drag the drawer on the Z axis. In this case that's to left and right. At start of the game the drawer is neatly inside the desk. You may not move the drawer further inside the desk, like you can't in real life. But you may also not drag the drawer too far out.

alt text

 //if current object moves too much out of position, it will be teleported back into its original position.
 
 var objectPosX : float;
 var objectPosY : float;
 var objectPosZ : float;
 var originalPos : Vector3;
 
 function Start() {
     //store all current position and rotation for the object
     objectPosX = transform.position.x;
     objectPosY = transform.position.y;
     objectPosZ = transform.position.z;
     originalPos = Vector3(objectPosX,objectPosY,objectPosZ);
 }
 
 function Update() {
 
     if(objectPosX < transform.position.x - renderer.bounds.size.x){
         transform.position = Vector3(objectPosX,objectPosY,objectPosZ);
         rigidbody.velocity = Vector3(0,0,0);
     }
 
     if(objectPosZ < transform.position.z - renderer.bounds.size.z){
         transform.position = Vector3(objectPosX,objectPosY,objectPosZ);
         rigidbody.velocity = Vector3(0,0,0);
     }
 
     if(objectPosX > transform.position.x + renderer.bounds.size.x){
         transform.position = Vector3(objectPosX,objectPosY,objectPosZ);
         rigidbody.velocity = Vector3(0,0,0);
     }
 
     if(objectPosZ > transform.position.z + renderer.bounds.size.z){
         transform.position = Vector3(objectPosX,objectPosY,objectPosZ);
         rigidbody.velocity = Vector3(0,0,0);
     }
 
 }
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
0
Wiki

Answer by NickP_2 · May 02, 2014 at 01:51 PM

This script will move an object between 2 positions, with the variable targetpos you don't really need a rigidbody for this tho, well I wont use one :P

 public float zOffset, speed;
 private Vector3 firstPos, maxPos, taretPos;
 
 void Start()
 {
     //firstPos will be the start position
     firstPos = transform.position;
     //maxPos will be the maximum position (in z) 
     maxPos= firstPos + new Vector3(0, 0, zOffset);
     //change the targetPos to a value you want
     targetPos = startPos;
 
 }
 
 void Update()
 {
     //before we try to move the object, we check if the target position is legit:
     // targetPos.z must be bigger than the startPos or smaller than the max position
     if(targetPos.z > startPos.z && targetPos.z < maxPos.z)
     {
         //this will smoothly move the object to its target position
         transform.position = Vector3.Lerp(transform.position, targetPos, Time.deltaTime * speed);
     }
 
 }

So basicly, what ever input you take care off, just change the targetPos to your desired value. I haven't tried this in unity or checked for compile errors, so let me know if there's anything wrong

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 derrtyones · May 02, 2014 at 01:57 PM 0
Share

Thanks for your quick help, but Sorry I use Javascript. I also need a rigidbody otherwise I can't drag the object.

avatar image
0

Answer by theredace · May 02, 2014 at 02:49 PM

I imagine the easiest way to go about this is to clamp the min and max positions of the drawer on the Z and X axes using Mathf.Clamp. Set the original position and use Vector3.Lerp or Vector3.MoveTowards or something similar to snap it back to it's original position when let go.

Comment
Add comment · 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

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

22 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

Related Questions

Rigidbody Disable Velocity/Movement? 1 Answer

How do I move this rigidbody? 1 Answer

OnTriggerEnter Or OnCollisionEnter? 3 Answers

Troublesome collision detection when changing rigidbody.velocity directly 0 Answers

Unity 2D Colliders not Colliding 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