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 Xysch · Oct 07, 2014 at 11:33 PM · movementplayermouserigidbody2d

Why does it seem that rigidbody seems of when moving the character?

What I have:

So I'm pretty new to C# but know a few other codes. Everything I am doing is in Unity2D. Now I have gotten my character to move using the mouse and that it has like a "lag" or "delay" and that the player moves faster the farther away your mouse is.

The Problem:

Now the only problem with this, is whenever the player collides with objects, the player seems to studder and jerk around. My player also doesn't seem to put an effect on Rigidbody2D's objects anymore. Although I can slightly push floating blocks, they don't spin and rotate anymore like they did before. I beleive this might be from using transform and not using a force like with the other code I used, but what do I know.

The C# Code:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerScript : MonoBehaviour {
     public float speed = 3.0f;
     public AudioClip thudSound;
     public bool mouseClick = false;
 
     private Vector3 targetPos;
     
     void Start() {
         targetPos = transform.position;    
     }
 
     //Thud Sound
     void OnCollisionEnter2D(Collision2D target){
                 var absVelX = Mathf.Abs (rigidbody2D.velocity.x);
                 var absVelY = Mathf.Abs (rigidbody2D.velocity.y);
                 if (absVelX <= .01f || absVelY <= .01f) {
                         if (thudSound)
                                 AudioSource.PlayClipAtPoint (thudSound, transform.position);
                 }
     }
     
     
     void Update () {
         //To turn on and off the follow mouse
         if (Input.GetMouseButtonDown (0)) {
             if(mouseClick == false){
                 mouseClick = true;
             } else if (mouseClick == true){
                 mouseClick = false;
             }
                 }
         //Follow the mouse
         if (mouseClick == false) {
                         float distance = transform.position.z - Camera.main.transform.position.z;
                         targetPos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, distance);
                         targetPos = Camera.main.ScreenToWorldPoint (targetPos);
         
                         transform.position = Vector3.Lerp (transform.position, targetPos, speed * Time.deltaTime);
                 } else {
             //Stop following but continue sliding
             transform.position = Vector3.Lerp (transform.position, targetPos, speed * Time.deltaTime);
                 }
     }
 }
 





I would really appreciate it if you could help me understand this and thanks for your time!

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
0
Best Answer

Answer by sevensixtytwo · Oct 08, 2014 at 12:18 AM

You really shouldn't use Vector3.Lerp and its cousins for movement when you're talking rigidbodies. Collisions get all screwy.

Try Rigidbody.MovePosition

 rigidbody.MovePosition(targetPos * 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Can someone help me with my TPS Controller/Camera? 1 Answer

player move while mouse move. 0 Answers

Player not moving 1 Answer

Vertical mouse input inverted when player is turned around 0 Answers

My 2D character is hovering away from the ground and walls for some reason, and is also experiencing some other issues,My 2D Player hovers over the Ground and Wall Tiles I have set up, don't know why 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