Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 xintaris · Mar 21, 2021 at 11:19 PM · 2d-physicsdynamicridgidbody

odd behavior with click to move and ridgedbody 2d.

this is in 2D when I use this script it works fine if it is not dynamic. I want to use physics and gravity. I add a ridgidbody and box collider to the player and freeze the Z axis so it wont rotate and box colliders to the ground. it still moves but now it does funny things. sometimes when it stops it starts to bounce, most of the time the moving is just weird and I don't know how to fix it. I have tried many scripts from google as well as watch videos from YouTube but none of them really go into 2D movement with dynamic ridgidbody. Any help would be nice.

          public float speed = 1.5f;
          private Vector3 target;
      
          void Start () {
              target = transform.position;
          }
          
          void Update () {
              if (Input.GetMouseButtonDown(0)) {
                  target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                  target.z = transform.position.z;
              }
              transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
 
 
 
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 toficofi · Mar 22, 2021 at 09:24 AM

When you are dealing with a dynamic Rigidbody, you will want to set the position on the Rigidbody instead of the transform so it can handle its physics stuff.


 public float speed = 1.5f;
 private Vector3 target;
 private Rigidbody2D rb;
 
 void Start () {
     target = transform.position;
     rb = GetComponent<Rigidbody2D>();
 }
 
 void Update () {
     if (Input.GetMouseButtonDown(0)) {
         target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         target.z = transform.position.z;
     }
     
     rb.position = Vector3.MoveTowards(rb.position, target, speed * Time.deltaTime);
 }
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
avatar image
0

Answer by xintaris · Mar 23, 2021 at 10:15 AM

Thanks for the answer but it still did not work. On the bright side I fixed with this code. Now I have one problem. The stop condition never seems to get called so he keeps moving. Is there a better way to do this?

 public float speed = 3f;
 
     private Vector3 target;
     private Rigidbody2D rb;
     private bool isMoving = false;
     private float dir;
     private Vector3 fingerPos;
    
 
 
     void Start()
     {
         target = transform.position;
         fingerPos = transform.position;
         rb = GetComponent<Rigidbody2D>();
 
     }
 
     void Update()
     {
 
         if (Input.GetMouseButtonDown(0))
         {
             fingerPos = Input.mousePosition;
             fingerPos.z = 10;
             fingerPos = Camera.main.ScreenToWorldPoint(fingerPos);  
         }
         checkPos(fingerPos);
 
 
     }
     private void FixedUpdate()
     {
         if (isMoving)
         {
             Move(dir);
         }
     }
 
     private void checkPos(Vector3 targetPos)
     {
         if (transform.position.x < targetPos.x)
         {         
             Debug.Log("less then moving right");
             dir = 1;           
             isMoving = true;          
         }
         if (transform.position.x > targetPos.x)
         {
             
             Debug.Log("greater then moving left");
             dir = -1;        
             isMoving = true;          
         }
         if (Vector3.Distance(transform.position, target) < 0.0001f)
         {
             Debug.Log("stopping");
             isMoving = false;
             fingerPos = transform.position;
         }
     }
 
     private void Move(float dir)
     {
         //sets value of X using dir and speed
         float xVal = dir * speed * 100 * Time.deltaTime;
 
         //creates a vector2 for the velocity
         Vector2 targetVelocity = new Vector2(xVal, rb.velocity.y);
 
         //sets players velocity
         rb.velocity = targetVelocity;
     }
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

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

Related Questions

Can I arbitrarily move a dynamic Rigidbody 2D? 1 Answer

2D Dynamic Reflections ReadPixel Issue 0 Answers

2D dynamic collider for dynamic shape 0 Answers

Cannot get 3D Text/Text Mesh to wrap or a attached collider used as a button to scale based on word count. 2 Answers

Creating a dynamic alpha based on cursor/object position 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