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 /
  • Help Room /
avatar image
0
Question by derrtyones · Jul 27, 2016 at 03:41 PM · 2djavascriptmovementdirection

2D enemy movement direction

I'm a rookie when it comes to coding and I'm making a simple top-down 2D game. I have an enemy which exists out of a sprite. I have 4 different sprites where the enemy faces different directions.

I'm using a pathfinding script (Poly|Nav in Asset Store) which moves the character to wherever I am clicking on my screen. This works fine, but I want the enemy sprite to change depending on which direction the enemy is facing. I have tried the following Javascript. The code does change the sprite but it never uses all of them. So the enemy might be facing to the right whilst going down, etc. Any ideas?

I did notice that for example when moving downwards + a bit to the left, it will try and face both down and left at the same time because both X and Y axis are changing. Is there a way to solve this? I thought maybe it's possible to calculate which number is more dominant and base it off that, just not sure how to do that and if that would work.

 #pragma strict
 var prevLoc : Vector3 = Vector3.zero;
 public var NPCMoveUp : Sprite;
 public var NPCMoveDown : Sprite;
 public var NPCMoveLeft : Sprite;
 public var NPCMoveRight : Sprite;
 
 InvokeRepeating("NPCMoving", 0, 2);
 
 function Update(){
      var curVel : Vector3 = (transform.position - prevLoc) / Time.deltaTime;
      
      if(curVel.y > 0){
         GetComponent(SpriteRenderer).sprite = NPCMoveUp;
      }
      
      if(curVel.y < 0){
         GetComponent(SpriteRenderer).sprite = NPCMoveDown;
      }
 
      if(curVel.x > 0){
          GetComponent(SpriteRenderer).sprite = NPCMoveRight;
      }
      
      if(curVel.x < 0){
          GetComponent(SpriteRenderer).sprite = NPCMoveLeft;
      }
      
  }
  
  function NPCMoving(){
     prevLoc = transform.position;
  }

Maybe it's not possible the way I am trying it right now? I don't know how to solve this. Thanks for your time and help.

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

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.

Follow this Question

Answers Answers and Comments

116 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

Related Questions

2D Top Down, Enemy facing player slow rotation issue. Vector gets messed up after collision. 3 Answers

Constraning an object's forces to only two directions in 2D (or infinite friction?) 0 Answers

Sprites moving (shaking) during animation while they should not 1 Answer

2D moving Script 1 Answer

Moving an object to a direction in degrees at a specific speed? 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