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 ghaddar0292 · May 24, 2013 at 06:15 PM · raycastaddforcepushpull

How can I improve my Push/Pull object script?

Hello everyone,

I have been trying to get a push/pull script to work and finally I got something to at least work. I am work on a 2D game, and I am trying to push/pull a cube on a ground. The following is my script so far:

 var hit : RaycastHit; //Holds some of the properties the raycast hit.
 var pushforce : float = 1; // Pushing Force
 var pullforce : float = 1; // Pulling Force
 var forceMode : ForceMode = ForceMode.VelocityChange; //Force type
 
 function Update () {
 
 //Get PlatformInputController script
 var rotate : PlatformInputController = GetComponent(PlatformInputController); 
 
 // Get CharacterMotor script
 var jump : CharacterMotor = GetComponent(CharacterMotor); 
 
 // Check for any objects at a distance of 1
 if (Physics.Raycast(transform.position, transform.forward,hit,1)) {
 
 // If tagged pushable and Action button is pressed
     if (hit.transform.gameObject.tag == "Pushable" && Input.GetButton("Action")){ 
     
     // Character cannot rotate 
     rotate.autoRotate = false; 
     
     //Character cannot jump
     jump.jumping.enabled = false; 
     
     // Get the distance between character and object position
     var D : Vector3 = transform.position - hit.transform.position; 
     
     // If on the left of object and move away from the object
     if (Input.GetAxis("Horizontal") < 0 && D.x < 0){
     // Apply pull only when the distance is greater than 0.5
     if (hit.distance>0.5){ 
     hit.rigidbody.AddForceAtPosition(D*pullforce, hit.transform.position, forceMode); 
     }
     }
     // If on the left of object and move towards the object
     if (Input.GetAxis("Horizontal") > 0 && D.x < 0){ 
     // Apply push only when the distance is less than 0.5
     if (hit.distance < 0.5){ 
     hit.rigidbody.AddForceAtPosition(-D*pushforce, hit.transform.position, forceMode);
     }
     }
     // If on the right of object and move towards the object
     if (Input.GetAxis("Horizontal") < 0 && D.x > 0){ 
     // Apply push only when the distance is less than 0.5
     if (hit.distance<0.5){ 
     hit.rigidbody.AddForceAtPosition(-D*pushforce, hit.transform.position, forceMode);
     }
     }
     // If on the right of object and move away from the object
     if (Input.GetAxis("Horizontal") > 0 && D.x > 0){ 
     // Apply pull only when the distance is greater than 0.5
     if (hit.distance>0.5){ 
     hit.rigidbody.AddForceAtPosition(D*pullforce, hit.transform.position, forceMode);
     }
     }
     }
     // Otherwise character can rotate and jump again
     else{ 
     rotate.autoRotate = true; 
     jump.jumping.enabled = true;
     }
 }
 }

This script works fine(I have commented it if there is still anything not clear please ask), but when in action it is not smooth at all. Two problems that I notice is that my force is dependent on the distance of my character to the cube hence sometimes I get a fast motion and sometimes I get a slow motion when I push or pull. The other problem I have is that when I have the rotation of the cube not fixed, if I try to push or pull the cube it will start moving by rotating forward or backward. At the end I need the rotation to be not fixed since I want to throw the cube of a ledge and it should rotate by gravity as it falls. So making the cube fixed rotation is not an option for me.

I am new to Unity and scripting and I hope if anyone can at least lead me to the right direction of thinking, or what methods I should use in order to have a more solid script.

Any help is appreciated.

Thank you very much.

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

1 Reply

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

Answer by Owen-Reynolds · May 24, 2013 at 07:27 PM

For the rotate, AddForceAtPosition is designed to rotate the object as if you were pulling/shoving it from a certain point. To not rotate an object extra, just use AddForce. It's like pushing someone on a swing, but making sure you do it dead-center, so they don't twist or flip.

For the force, the standard trick is to use the normalized distance vector (which is often call direction.) Very common trick for moving, and lots of places to look up an explanation better than mine.

In the above, D is the entire line between puller and pulled. If you pull based on D, clearly something twice as far away will get pulled twice as hard. D.normalized is D reduced to a standard-length arrow pointing the way to go. Normalizing "takes away" the distance, leaving just direction.

Comment
Add comment · Show 4 · 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 ghaddar0292 · May 24, 2013 at 11:38 PM 0
Share

I have done the adjustments you suggested and the results are the same. The code is the following now:

 var hit : RaycastHit; //Holds some of the properties the raycast hit.
 var pushforce : float = 1; // Pushing Force
 var pullforce : float = 1; // Pulling Force
 var force$$anonymous$$ode : Force$$anonymous$$ode = Force$$anonymous$$ode.VelocityChange; //Force type
 
 function Update () {
 
 //Get PlatformInputController script
 var rotate : PlatformInputController = GetComponent(PlatformInputController); 
 
 // Get Character$$anonymous$$otor script
 var jump : Character$$anonymous$$otor = GetComponent(Character$$anonymous$$otor); 
 
 // Check for any objects at a distance of 1
 if (Physics.Raycast(transform.position, transform.forward,hit,1)) {
 
 // If tagged pushable and Action button is pressed
     if (hit.transform.gameObject.tag == "Pushable" && Input.GetButton("Action")){ 
     
     // Character cannot rotate 
     rotate.autoRotate = false; 
     
     //Character cannot jump
     jump.jumping.enabled = false; 
     
     // Get the distance between character and object position
     var D : Vector3 = transform.position - hit.transform.position; 
     
     // If on the left of object and move away from the object
     if (Input.GetAxis("Horizontal") < 0 && D.x < 0){
     // Apply pull only when the distance is greater than 0.5
     if (hit.distance>0.5){ 
     hit.rigidbody.AddForce(D.normalized*pullforce, force$$anonymous$$ode); 
     }
     }
     // If on the left of object and move towards the object
     if (Input.GetAxis("Horizontal") > 0 && D.x < 0){ 
     // Apply push only when the distance is less than 0.5
     if (hit.distance < 0.5){ 
     hit.rigidbody.AddForce(-D.normalized*pushforce, force$$anonymous$$ode);
     }
     }
     // If on the right of object and move towards the object
     if (Input.GetAxis("Horizontal") < 0 && D.x > 0){ 
     // Apply push only when the distance is less than 0.5
     if (hit.distance<0.5){ 
     hit.rigidbody.AddForce(-D.normalized*pushforce, force$$anonymous$$ode);
     }
     }
     // If on the right of object and move away from the object
     if (Input.GetAxis("Horizontal") > 0 && D.x > 0){ 
     // Apply pull only when the distance is greater than 0.5
     if (hit.distance>0.5){ 
     hit.rigidbody.AddForce(D.normalized*pullforce, force$$anonymous$$ode);
     }
     }
     }
     // Otherwise character can rotate and jump again
     else{ 
     rotate.autoRotate = true; 
     jump.jumping.enabled = true;
     }
 }
 }

Any other ideas how I can avoid the cube to rotate? And any Idea how I can smoothen the pull to me or push forward?

Thanks for your reply.

avatar image Owen-Reynolds · May 25, 2013 at 12:05 AM 0
Share

Normalizing should be a huge visible difference. It may still not work correctly, but if a typical cube is 10 away, suddenly pulls should be 1/10th as much (which would be adjusted with pull force.)

Cubes will naturally tend to stick and roll over, no matter how you push then, just because of friction. It usually looks pretty cool (I pulled too hard -- it's tipping!) $$anonymous$$aybe test with friction=0. If that was the case, can play with angularDrag, swapping physics mats... . These are things you can test in a demo scene just by watching cubes slide down ramps.

Can also mess with using AddForceAtPos to push them near the bottom, but a giant pain and they can tip the other way.

avatar image ghaddar0292 · May 25, 2013 at 12:10 AM 0
Share

Alright, I guess ill just have to play around until i've got it right. Thank you for your help.

avatar image ghaddar0292 · May 26, 2013 at 12:33 AM 0
Share

After playing with my script I noticed that I had to normalize my D before adding the force. Push/Pull are now much smoother but still contain some problems that I hope to be fixing soon. Thank you for your help.

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

14 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

Related Questions

pulling objects across a collider 0 Answers

sucking in particles 3 Answers

pulling in objects one end, and releasing out the other 0 Answers

Add Force based on Raycast 1 Answer

Trouble with Ray & Raycasting and AddForce() 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