Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 GameCreater562 · Nov 22, 2018 at 05:50 PM · animationmovementanimatortilemap

Problems with animation and movement

Hello, I'm currently trying to make my player character move around the map, but for some reason, it will not move left and right (it will play the animation trigger however) and it can only move up and down in a restricted space. Currently my code for movement looks like this: public class PlayerMovement : MonoBehaviour {

 [SerializeField]
 private float speed;

 [SerializeField]
 private Animator animator;

 private void FixedUpdate()
 {
     float moveHorizontal = Input.GetAxis("Horizontal");
     float moveVertical = Input.GetAxis("Vertical");

     Vector2 currentVelocity = gameObject.GetComponent<Rigidbody2D>().velocity;


     float newVelocityX = 0f;
     if (moveHorizontal < 0 && currentVelocity.x <= 0)
     {
         newVelocityX = -speed;
         animator.SetInteger("DirectionX", -1);
     }
     else if (moveHorizontal > 0 && currentVelocity.x >= 0)
     {
         newVelocityX = speed;
         animator.SetInteger("DirectionX", 1);
     }
     else
     {
         animator.SetInteger("DirectionX", 0);
     }

     float newVelocityY = 0f;
     if (moveVertical < 0 && currentVelocity.y <= 0)
     {
         newVelocityY = -speed;
         animator.SetInteger("DirectionY", -1);
     }
     else if (moveVertical > 0 && currentVelocity.y >= 0)
     {
         newVelocityY = speed;
         animator.SetInteger("DirectionY", 1);
     }
     else
     {
         animator.SetInteger("DirectionY", 0);
     }

     gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(newVelocityX, newVelocityY);
 }

I'm not entirely sure where I went wrong, and why my character isn't moving left and right. I'm wondering if it has something to do with my map, since all objects are on one layer, meaning the ground and collidable objects such as trees have sort of overlapped, but I don't know how to separate them into different layers.

If anyone knows how to help, I'd really appreciate it.

Comment
Add comment · Show 2
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 ecv80 · Nov 23, 2018 at 12:06 AM 0
Share

Intriguing

avatar image mlnczk · Nov 23, 2018 at 01:14 PM 0
Share

Does your player's rigidbody has freezed positions of some axis?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tristantcate · Nov 23, 2018 at 01:57 PM

Hi GameCreater562,


In terms of functionality, there's nothing wrong with your script. You should be able to move left and right. It probably has to do with colliders like you said.


Luckily you can easily modify what collides with what in Unity. I refer to layer system. You can give every collider a layer just as you would a tag. Then if you go to Edit > Project Settings > Physics2D (considering you're working in a 2D project).

alt text

Then you'll get a collision matrix. Shown below is default. You will see your own custom added layers in here as well.


alt text

In here you can simply uncheck the boxes for the objects you wouldn't want to collide with one another (for instance if there are colliders on things you don't want to collide with the player).


Let me know if this helped, and good luck on your project :)


phy2d.png (33.8 kB)
phy2d-2.png (3.0 kB)
Comment
Add comment · Show 1 · 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 GameCreater562 · Nov 29, 2018 at 05:19 PM 0
Share

Thanks for your help, I was just wondering how I can sort my tiles into layers?

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

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

Animator behaviour on GameObject 1 Answer

Animation Lag when Changing Movement Direction 2D (4.3) 2 Answers

Mixamo strafing animation moving slightly on the Z-Axis 0 Answers

Animator & movements 1 Answer

Twin-Stick Movement 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