• 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
Question by IckyZombie · Feb 19, 2018 at 12:46 AM · movement2d gamesmooth

How can I smooth out 2D object movement?

Hello, I am a VERY new developer, so I apologize if this is a basic question, but I have scoured the site for an answer and cant seem to find the solution.

I am trying to create an enemy script that will have the enemy walk around randomly, which I have sort of accomplished, but he snaps around to each random position instead of smoothly moving to the position. I have tried several iterations with lerp and transform and translate, but nothing seems to work!

Here is the closest I have come so far:

 public class EnemyBehavior : MonoBehaviour {
 
     public float Speed;
     private Rigidbody2D rb2d;
 
     void Start() {
 
         rb2d = GetComponent<Rigidbody2D> ();
         StartCoroutine(Wait());
     }
 
   private IEnumerator Wait() {
             
         float randomX;
         float randomY;
 
         randomX = Random.Range (-1, 2);
         randomY = Random.Range (-1, 2);
 
         Vector2 movement = new Vector2 (randomX, randomY); 
 
         rb2d.MovePosition (rb2d.position + movement * Time.deltaTime * Speed);
 
         yield return new WaitForSeconds(2f);
 
         StartCoroutine(Wait());
 
         }
 
 }

This code will make the enemy move to a new position within 1 tile every 2 seconds. The problem is he just "appears" there and I want it to seem smooth like he is walking.

Any help would be greatly appreciated!

Comment

People who like this

0 Show 1
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 PersianKiller · Feb 19, 2018 at 05:52 AM 0
Share

hi, check this it might be helpful :)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 public class healthManager : MonoBehaviour {
 private Rigidbody2D rb2d;
 void Start() {
     rb2d = GetComponent<Rigidbody2D> ();
     StartCoroutine(Wait());
 }

 private IEnumerator Wait() {
     rb2d.velocity = new Vector2 (UnityEngine.Random.Range(-1,2),UnityEngine.Random.Range(-1,2));
     yield return new WaitForSeconds(2f);
     StartCoroutine(Wait());
 }
 }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

178 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 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

sprite bug: animator doesnt work and it gets bigger 0 Answers

Object movement algorithm 0 Answers

Can't make non-smooth movement script 0 Answers

Detecting missing collision in 2D platformer to change direction for enemy - without Raycast 1 Answer

Sprite shaking/ jittering/ jiggling/ wiggling/ vibrating when moving it using joystick (2D). 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