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 /
  • Help Room /
avatar image
0
Question by Trensharo · Oct 22, 2016 at 03:38 PM · collisionmovement scripttransform.translatejittering

Transform.Translate snaps back in FixedUpdate

I am currently toying with movement solutions in C#. I am seeking instantaneous velocity increases with no friction against walls, and transform.Translate produces the most desirable results.

I have a Capsule with a rigidbody and the script as the player. The world consists of a plane and a box with static colliders.

Moving my code from Update to FixedUpdate solved most of the jitteriness, but there is a persistent snapping back whenever the forward key is released.

Here is the code:

 public float speed = 10.0f;
 public float rotationSpeed = 100.0f;
 
 private Vector3 translation = new Vector3();
 
 void FixedUpdate()
     {
         translation = Vector3.zero;
 
         if (Input.GetKey(KeyCode.W))
             translation += Vector3.forward;
         if (Input.GetKey(KeyCode.S))
             translation += Vector3.back;
         if (Input.GetKey(KeyCode.Q))
             translation += Vector3.left;
         if (Input.GetKey(KeyCode.E))
             translation += Vector3.right;
         if (Input.GetKey(KeyCode.A))
             transform.Rotate(0, -rotationSpeed * Time.deltaTime, 0);
         if (Input.GetKey(KeyCode.D))
             transform.Rotate(0, rotationSpeed * Time.deltaTime, 0);
 
         translation = translation.normalized  * speed;
         transform.Translate(translation * Time.fixedDeltaTime);
     }

This produces the exact result I am looking for aside from the snapback upon key release. The capsule has an instant acceleration and moves smoothly against the box, regardless of the angle. The Rigidbody Velocity solutions result in an undesirable level of "stick" to the box collider.

Any insight to helping me achieve a resolution would be greatly appreciated :)

Comment
Add comment · Show 5
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 hexagonius · Oct 22, 2016 at 08:35 PM 0
Share

What do you mean by snapback? Only upon releasing forward key?

avatar image Trensharo hexagonius · Oct 31, 2016 at 07:26 AM 0
Share

@hexagonius - Yes. In the frame immediately after releasing the movement keys the physics engine bumps the capsule's collider back a single frame worth of movement.

avatar image conman79 · Oct 22, 2016 at 09:49 PM 1
Share

You'll often get weird results when using transform.Translate/Rotate and rigidbodies together. I'd highly recommend using Rigidbody.$$anonymous$$ovePosition and Rigidbody.$$anonymous$$oveRotation ins$$anonymous$$d. They do almost the same thing as translate and rotate, but within the physics engine. You should be able to achieve identical results without the bugginess.

avatar image Trensharo conman79 · Oct 31, 2016 at 07:25 AM 0
Share

@conman79 - I got identical results using the RigidBody.$$anonymous$$ovePosition function, while changing the vector maths to use transform.forward and transform.right.

This is my solution with your suggestion. Again, identical results. I am bumping back upon releasing W

 void FixedUpdate()
     {
         translation = Vector3.zero;
 
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))
             translation += transform.forward;
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.S))
             translation += -transform.forward;
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Q))
             translation += -transform.right;
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.E))
             translation += transform.right;
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
             transform.Rotate(0, -rotationSpeed * Time.deltaTime, 0);
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
             transform.Rotate(0, rotationSpeed * Time.deltaTime, 0);
 
         translation = translation.normalized  * speed;
         //transform.Translate(translation * Time.fixedDeltaTime);
 
         GetComponent<Rigidbody>().$$anonymous$$ovePosition(transform.position + translation *Time.fixedDeltaTime);
     }

I ran into this problem in DarkBASIC, and the solution was simply to run the physics-collision calculations immediately before the screen synced via a frame render.

I am thinking the solution may lie in running the movement in another $$anonymous$$onoBehavior function... I have tried OnPreRender() but the Ridigbody.$$anonymous$$ovePosition and transform.Translate functions don't seem to work within that function.

A way to disable the friction against the wall object would allow me to avoid this syncing problem altogether. The physics engine does things that I don't want it to do, which is why RigidBody.SetVelocity is producing undesirable results. I am still new and not incredibly proficient with the nuances of manipulation Rigidbody settings beyond the inspector settings.

avatar image Trensharo · Nov 01, 2016 at 04:25 PM 0
Share

I guess a better description of the issue is this:

While holding the forward key, the character is moving after the screen sync and appearing deeper in the wall than he should. When you stop holding forward, the character is no longer constantly being pressed into the wall and comes to rest against the wall where he should be appearing the whole time. This appears as the character being bumped out away from the wall, but is actually a lack of appearing closer than he should.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Character Controller jittering with other colliders 0 Answers

Need help with high speed collisions, sanity dwindling 1 Answer

Movement with CharacterController is lagging while moving backward 0 Answers

Can someone help me fix this? 0 Answers

move a player at specific distance to road lanes 0 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