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 T1Green · May 27, 2014 at 11:26 AM · 2dmovementcharactermotionteleporting

How to move 2D character from one place to another by "movement" and not "teleporting"?

Hi everyone

Introduction

I am making some controls for my character who moves only left and right on the x-axis. I posted the code for his movement to left and right. The idea is to have him only move those widths in the game so there's like 5 places he can be on the screen.

 public float fieldWidth = 3;
 
 void FixedUpdate(){
     if (Input.GetKeyDown(KeyCode.LeftArrow)){
         transform.Translate(-fieldWidth, 0,0f, 0,0f);
     }
 
     if (Input.GetKeyDown(KeyCode.RightArrow)){
         transform.Translate(fieldWidth, 0,0f, 0,0f);
     }
 }

Problem:

The main problem is that when I used translate in the code then the character "teleports" to those places. It's cool that he can only move 5 places in total with 3.0f each step BUT I wanted to have him "move" those places instead of "teleporting".

After all, I want to add an animation on the character when he moves from one place to another.

Alternative tries:

If I use deltaTime for his movement/velocity then he just moves at that speed and do not stop at one of the 5 places as he does now and is supposed to do.

Goal:

I want him to move 3.0f every time I press Right or Left arrowkey but he should show motion from one place to another instead of teleporting one place to another.

Question:

Could someone help me showing what I'm doing wrong, and what I should do to correct it?

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

Answer by vptb · May 27, 2014 at 03:14 PM

There's actually several ways to do this, on my opinion you should go with adding a rigidbody2D to you character and move it with velocity.

     private Vector3 _destinyPos;
     private float _distanceTolerance = 0.1f;
     public float fieldWidth = 3;
     public float speed = 3;
 
     private void Update(){
         if (Input.GetKeyDown(KeyCode.LeftArrow)){
             _destinyPos = transform.position - new Vector3(fieldWidth, 0, 0);
         }
 
         if (Input.GetKeyDown(KeyCode.RightArrow)){
             _destinyPos = transform.position + new Vector3(fieldWidth, 0, 0);
         }
     }
 
     private void FixedUpdate(){
         if (Mathf.Abs(_destinyPos.x - transform.position.x) > _distanceTolerance){
             if (_destinyPos.x < transform.position.x) rigidbody2D.velocity = -transform.right * speed;
             else rigidbody2D.velocity = transform.right * speed;
         }
         else rigidbody2D.velocity = Vector2.zero;
     }


Receiving Input should always be done on the Update() function. The FixedUpdate() should be used for physics calculations.

Another option is to move him with transform.Translate(). But after some experience recommend using rigidbody.

Comment
Add comment · Show 2 · 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 siaran · May 27, 2014 at 05:38 PM 0
Share

Why would it be better to use rigidbody?

avatar image T1Green · May 27, 2014 at 06:07 PM 0
Share

It worked nicely. Had to move the variables a little up and here and there but it worked wonders. Thanks.

I also added a

if ($$anonymous$$athf.Round(destinyPos.x) == $$anonymous$$athf.Round(targetPosition.x)){}

inside the input if-loop to lock the movements while the character is moving. Otherwise it would be like jumping again in the air while he should only be able to do jump when he's on the ground. (Sorry, the other analogy didn't work right)

avatar image
0

Answer by siaran · May 27, 2014 at 01:51 PM

You should determine your player's target position when you press a key, then move him in the direction of your target position (3.0f in some direction from your current position) over time until he has arrived there. Easiest way is probably using Vector2.MoveTowards.

keep in mind that you should make sure that if a key is pressed while the player is moving it the target position is not changed (or changed to a position where you want your player to be able to go depending on what kind of behaviour you want)

Comment
Add comment · Show 3 · 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 T1Green · May 27, 2014 at 02:08 PM 0
Share

It seems that I'll need to insert both the current and the future position into 2 variables and then use those as to not get a new current position all the time.

Thanks, I'll try to look at it when I get home today.

avatar image siaran · May 27, 2014 at 02:26 PM 0
Share

you only need to keep the future position in a variable, and keep a boolean "is$$anonymous$$oving". set to true when you press a key and set to false when the destination is reached, then only accept a new target position when is moving is false.

avatar image T1Green · May 27, 2014 at 06:04 PM 0
Share

Thanks, I tried it but I couldn't get it to work right. Dunno why. Still I believe it the logic is right. I went with a modified version of vptb's version.

Interesting, you can't have two right answers...but this one is also right.

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

22 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

Related Questions

Background & sprites are smearing towards the player during movement. Help determine the cause and solution? 0 Answers

How can I move the character like 2D games? 1 Answer

Unity 4.3 2D make character not so blurry when moved 1 Answer

my 2d character won't move when i hold any movement key? 1 Answer

Locking movement to 2d 1 Answer


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