• 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 /
This question was closed Aug 29, 2016 at 06:46 PM by Mineck for the following reason:

The question is answered, right answer was accepted

This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by Mineck · Aug 27, 2016 at 08:42 PM · 2dscripting problemmovement2d game2d animation

Script to animation and movement of 2d player

Hi ! I new to game development and I found a tutorial for a script for movement of 2d player, I copied it, my animation works but my player doesn't move, I try to fix it. So this is the original script :

using UnityEngine; using System.Collections;

public class Playermovement : MonoBehaviour {

 Rigidbody2D rbody;
 Animator anim;

 // Use this for initialization
 void Start () {

     rbody = GetComponent<Rigidbody2D> ();
     anim = GetComponent<Animator> ();

}

 // Update is called once per frame
 void Update () {

     Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

     if (movement_vector != Vector2.zero) {
         anim.SetBool ("iswalking", true);
         anim.SetFloat ("Input_x", movement_vector.x);
         anim.SetFloat ("Input_y", movement_vector.y);
     }
     else {
         anim.SetBool ("iswalking", false);
         }
     rbody.MovePosition (rbody.position + movement_vector * Time.deltaTime);
     }
 }

This is the link to the tutorial : https://www.youtube.com/watch?v=XZDjkQ8wEd0&list=PL_4rJ_acBNMH3SExL3yIOzaqj5IP5CJLC∈dex=4

and I try to fix it with replace void Update by void FixedUpdate as explained comments

Comment
Add comment · Show 4
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 DavidWatts · Aug 28, 2016 at 05:31 AM 0
Share

that is odd it should be at least doing some movement. But you are correct in putting it in FixedUpdate but if you are using FixedUpdate you also need to use Time.fixedDeltaTime

avatar image Mineck DavidWatts · Aug 28, 2016 at 06:32 AM 0
Share

I corected my mistake but I still have the same problem

avatar image DavidWatts Mineck · Aug 28, 2016 at 06:42 AM 0
Share

do you have a Rigidbody2D attached and if so maybe check to see if the constraints are set on it

Show more comments

1 Reply

  • Sort: 
avatar image
0
Best Answer Wiki

Answer by Mineck · Aug 29, 2016 at 06:43 PM

Finally, watching other script and trying, I fix my script. I thank you for trying to help me. So this is the new script with correction for those who have the same problem:

 using UnityEngine;
 using System.Collections;
 
 public class Playermovement : MonoBehaviour {
 
     public float speed = 18;
     Rigidbody2D rbody;
     Animator anim;
 
     // Use this for initialization
     void Start () {
 
         rbody = GetComponent<Rigidbody2D> ();
         anim = GetComponent<Animator> ();
 
     
     }
 
     void FixedUpdate () {
 
         Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"))*speed* Time.fixedDeltaTime;
 
         if (movement_vector != Vector2.zero) {
             anim.SetBool ("iswalking", true);
             anim.SetFloat ("Input_x", movement_vector.x);
             anim.SetFloat ("Input_y", movement_vector.y);
         }
         else {
             anim.SetBool ("iswalking", false);
         }
         rbody.MovePosition (rbody.position + movement_vector);
         }
     }
 

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

112 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

Related Questions

Movement works in all directions except one? 0 Answers

My 2d movement script isn't working 0 Answers

Adding small acceleration to 2d top down movement game? 0 Answers

Object reference not set to an instance of an object (Unity 2D animation) 1 Answer

Why does it say The type or namespace 'BoardManager' could not be found. Are you missing 'Completed' using directive? 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