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 Kensei · Sep 29, 2014 at 08:36 PM · c#movementrigidbody2dgrid

Lerping on rigidbody2d.position isn't working properly, is it supposed to?

Hi guys, I have a 2D game where I need grid-based movement behavior. I have written this code, and it seem to work just fine when I used transform.position. However that didn't register collisions, so I tried rigidbody2d.MovePosition, that resulted in a continuous movement and was not what I wanted. So I scraped that and decided to lerp the rigidbody position, problem is that it just sometimws works and sometimes it doesn't. The code executes till the end without errors but the behavior says otherwise. Here's the code.

 public class Tile : MonoBehaviour
 {
     public float itemScore;
     public float smooth = 2;
 
 
     private bool moving = false;
     void FixedUpdate()
     {
         Control ();
     }
 
     void Control ()
     {
         //If the tile isn't moving, take the player input and store it in a 2D vector. If that vector
         //is not zero, start the movement coroutine.
         if(!moving)
         {
             Vector2 input = new Vector2 (Input.GetAxisRaw("Horizontal"),Input.GetAxisRaw("Vertical"));
             if (Mathf.Abs(input.x) > Mathf.Abs(input.y)) 
             {
                 input.y = 0;
             } else 
             {
                 input.x = 0;
             }
             if(input != Vector2.zero)
             {
                 StartCoroutine(Move (input));
             }
         }
     }
 
     private IEnumerator Move (Vector2 input)
     {
         float t = 0;
         moving = true;
         Vector2 startPos = rigidbody2D.position;
         Vector2 targetDelta = new Vector2 (input.x * GameController.GM.distanceDelta,input.y * GameController.GM.distanceDelta);
         Vector2 targetPos = startPos + targetDelta;
         while(rigidbody2D.position != targetPos)
         {
             t += Time.deltaTime * smooth;
             rigidbody2D.position = Vector2.Lerp(startPos,targetPos,t);
             yield return null;
         }
         moving = false;
         yield return null;
     }

So any tips?

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

Answer by Sausagesauce · Sep 29, 2014 at 09:39 PM

I'd recommend not using Vector2.Lerp for this. Use Vector2.MoveTowards instead. Example:

 //Speed per frame for movement
 var Step : float;
 //Moving?
 var MoveToPos : boolean;
 //Target
 var target;
 
 function Update(){
   if(MoveToPos){
     //If you are moving, move towards position. Define target on click.
     transform.position = Vector2.MoveTowards(transform.position, target.position, Step);
   }
 }


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 Kensei · Sep 30, 2014 at 04:02 PM 0
Share

isnt movetowards the same as lerp? Difference is that movetowards is limiting the speed. Also you can't call vector2 functions on a transform.position since its of vector3 type. I can't get it to work but I think that even if I did this would not register collisions.

avatar image
0

Answer by Anxo · Sep 29, 2014 at 10:08 PM

The hole point of a rigid body is to let physics control the motion, you never really want to tell a rigidbody where to be positioned but if you do, you would tell the transform, not the rigidbody. In case you wish to move the rigidbody to register collisions and not go through things, you either add force or if you need to control it more as your questions states (grid based) , you can use velocity.

  while(transform.position != targetPos)
         {
   
          rigidbody2d.velocity = transform.position - targetPos;
            yield return new WaitForEndOfFrame();
         }

Unchecked code may bug out but you get the idea, apply any smoothing you like.

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

27 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Is it ok to use Linear Drag and/or Mass to make an object immovable? 2 Answers

Enemy shouldn't walk with the player 2 Answers

Rigidbody2D.Moveposition not working 2 Answers

Distribute terrain in zones 3 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