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 quimqu · May 19, 2014 at 06:16 PM · collisionrigidbodydragslide

How to move a tank?

Hello,

I have a sort of commando game. I have a tank with a rigidbody and a box collider. I want the tank to move at a constant speed and I want that nothing moves the tank from its moving direction.

I tried to move it with rigidbody.AddRelativeForce as follows, but when it collides with another rigidbody, it "slides" from the collision point. If I increase the drag value, I can't move the rigidbody ig I don't increase the acceleration, but the slide problem does not disappear.

Code is:

 var maxSpeed : float = 1.1;
 var accel : float = 100000.0;
 
 function FixedUpdate () {
 
     if(rigidbody.velocity.magnitude <= maxSpeed) {
         rigidbody.AddRelativeForce(Vector3.forward*accel);
     }
 
 }

With Mass 10000 and Drag 10 it moves OK, but when collides, it slides.

I tried also with rigidbody.velocity but no way. As I increase the drag value I cannot move the rigidbody.

Any solution for this problem. I am sure I am doing something wrong, but I can't see what.

Thank you!

Comment
Add comment · Show 2
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 Cherno · May 19, 2014 at 07:51 PM 0
Share

Whenever the tank stops moving, set the rigidbody to Is $$anonymous$$inematic = true, and to false if he starts moving again.

avatar image Tanshaydar · May 19, 2014 at 08:22 PM 0
Share

$$anonymous$$ake it just a collider and update its transform values?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by DMGregory · May 19, 2014 at 10:31 PM

Edit: Here's what I found worked...

Rigidbody & joint configuration

  • Rotation is locked on the Y axis, so collisions can't turn the tank to the side

  • A configurable joint is added with no Connected Body (which means it connects to the world)

  • You can ignore the "Connected anchor" - that will be auto-configured for you

  • Set XMotion to Locked and the others to Free

  • The rest of the settings you can leave at their default values.

This will keep the object locked in its current YZ plane. If you need to rotate the object, manually adjust the connected anchor like so beforehand to keep the joint from freaking out:

 ConfigurableJoint joint = GetComponent<ConfigurableJoint>();
 joint.connectedAnchor = transform.TransformPoint(joint.anchor);

Comparison of with and without joint

/End Edit - below are the original suggestions


You can try to cancel-out sideways movement: (this didn't work)

 Vector3 sidewaysVelocity = Vector3.Dot(rigidbody.velocity, transform.right) * transform.right;
 
 rigidbody.AddForce(-sidewaysVelocity, ForceMode.VelocityChange);

I'm not sure whether it would suffice to do this every FixedUpdate, or if you might need to do this in OnCollisionEnter/Stay/Exit to override velocity changes during collisions.

Another option would be to use a ConfigurableJoint to constrain the tank to the world coordinate space, allowing free movement in the local YZ plane but not X. Not sure how well that would play with turning the tank though.


jointconfig.png (25.0 kB)
tanksjoint.gif (401.3 kB)
Comment
Add comment · Show 2 · 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 quimqu · May 22, 2014 at 03:45 PM -1
Share

Hi,

I tried your solutions but they didn't work.

Shouldn't it be simply a matter of mass? If I have a huge gameobject with a huge mass... it shouldn't move that easily...

Thank you

avatar image DMGregory quimqu · May 24, 2014 at 01:10 PM 0
Share

You should use comments to reply, rather than creating a new answer that doesn't contain an answer. ;)

$$anonymous$$ass alone won't solve the problem - it will make the sliding smaller, but not zero, so it can still build up over time. Having drastically different masses collide can sometimes result in unexpected physics behaviour too.

Did you try the configurable joint? I just made a test scene and it worked perfectly - much better than mucking with velocities.

I'll add the settings I used, in case that helps.

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

How to get movable/pushable cubes right? 0 Answers

How to stop Kinematic Rigidbody from moving through walls 2 Answers

Drag Rigidbody that respects collisions. 1 Answer

Objects with colliders going through walls and each other. 3 Answers

Problem with Object collision 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