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 jamesman135 · Jun 12, 2014 at 08:05 PM · c#animation2denemyai

Enemy AI Animation Flip when Player is on the right hand side

Basically I have a simple 2D platform game and the enemy follows the player, but when the player is on the right of the enemy, it walks backwards so instead of this I want to say this code:

if the player is on the right hand side of the enemy flip the animation,

otherwise leave it as it is.

I'm very new to programming and this enemy ai script is from one of the forums, this is it so far:

using UnityEngine; using System.Collections;

public class EnemyAi : MonoBehaviour {

 public Transform Target;
 private GameObject enemy;
 private GameObject player;
 private float Range;
 public float Speed;

 
 // Use this for initialization

 void Start () {
     enemy = GameObject.FindGameObjectWithTag ("Enemy");
     player = GameObject.FindGameObjectWithTag ("Player");
 }
 
 // Update is called once per frame
 void Update () {
     Range = Vector2.Distance (enemy.transform.position, player.transform.position);
     if (Range <= 8f) {
         transform.Translate(Vector2.MoveTowards (enemy.transform.position, player.transform.position, Range) * Speed * Time.deltaTime);
     }

} }

Thanks for your time, I'm sure it's something so simple but I can't find it anywhere, and I know you can flip the animation because I did it for my player, so I just need to know how to do it for the enemy.

Also im using c♯.

Thanks everyone.

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 CamoDragon042 · Jun 12, 2014 at 08:36 PM 1
Share

Try this and see if it works out.

 void Update(){
  Vector2 sides = Vector2.right;
  Vector2 playerPosistion = player.transform.position - transform.position;
 
  if(Vector2.Dot(sides, playerPosition) < 0){
    //flip the animation one side
  }
  else{
    //flip the animation to the other side
  }
 }
avatar image jamesman135 · Jun 16, 2014 at 12:05 PM 0
Share

Thank you for your time in replying and helping, unfortunately that didn't work, I got 5 errors in the console that read:

Socket: unable to join multicast group, error: Address already in use(48)

Unable to join player connection multicast group.

Assets/Scripts/EnemyAi.cs(30,47): error CS0103: The name playerPosition' does not exist in the current context Assets/Scripts/EnemyAi.cs(30,36): error CS1502: The best overloaded method match for UnityEngine.Vector2.Dot(UnityEngine.Vector2, UnityEngine.Vector2)' has some invalid arguments

Assets/Scripts/EnemyAi.cs(30,36): error CS1503: Argument #2' cannot convert object' expression to type `UnityEngine.Vector2'

I get the playerPosition one because it's not defined anywhere in the script, I did try changing it to try and say "if the gameobject with tag "player" is < 0" but that did nothing.

I'm still stuck, I appreciate the help though, thank you!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2d Top Down Enemy Control (LoZ type) 0 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

2D Animation does not start 1 Answer

How can I mirror 2d animation 2 Answers

Reusing animation but they sync 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