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 Silentones · Mar 19, 2019 at 02:46 AM · unity 2denemy aitargetingflipping

How do I flip an enemy that's automatically walking and targeting the player?

My player is able to jump over the enemies. They are able to target the enemy, but when he jumps over them. the enemies do not face the player when they are running after it again.
The way I have it causes this glitch Youtube-Game Glitch Video

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Opossum : MonoBehaviour
 {
     public float movespeed;
     private Rigidbody2D posbody;
     private Transform TPlayer;
 
     private bool facingleft = true; //faceplacment
 
     void Awake()
     {
         posbody = GetComponent<Rigidbody2D>();
     }
     // Start is called before the first frame update
     void Start()
     {
         
         TPlayer = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
     }
 
     // Update is called once per frame
     void Update()
     {
         Flip();
         transform.position = Vector2.MoveTowards(transform.position, TPlayer.position, movespeed * Time.deltaTime);
     }
 
     void Flip()
     {
         if (facingleft = !facingleft)
         {
             Vector3 oposcale = transform.localScale;
             oposcale.x *= -1;
             transform.localScale = oposcale;
         }
        
     }//Flip
 
 }//Opossum
 


Comment
Add comment · Show 1
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 highpockets · Mar 19, 2019 at 08:04 AM 0
Share

if(facingLeft = !facingLeft) should produce an error.. you need to use == operator, but in this case you can simply use if(!facingLeft)

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by highpockets · Mar 19, 2019 at 08:08 AM

I don’t think you need that facingLeft variable at all. You can just test like so:

 if(enemy.transform.position.x >= player.transform.position.x){
 //face left
 }else
 {
 //face right
 }
Comment
Add comment · Show 8 · 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 highpockets · Mar 19, 2019 at 08:12 AM 0
Share

I put //face left and //face right because I don’t know which x direction is positive 1 or -1 for your sprite, but a simple test will sort that out

avatar image Silentones highpockets · Mar 20, 2019 at 04:11 AM 0
Share

It still glitches out when I put in that code. $$anonymous$$y enemy sprite starts out facing left so it starts at 5. (enemy is to the right of me. That is why his X-axis is higher than $$anonymous$$e atm) so my first if statement is saying if enemy.x is greater than player.x *-1 so -5 X -1 = 5 which should face it to the right. I checked in the inspection manager and I can see it constantly flipping between -5 and 5.... When I make my player.x higher than the enemy is when the enemy grows a 2nd face to face my player character. CharPlacement

     void Flip()
     {
         if (posbody.transform.position.x >= TPlayer.transform.position.x)
         {
             Vector3 oposcale = transform.localScale;
             oposcale.x *= -1;
             transform.localScale = oposcale;
 
         } /*else if (posbody.transform.position.x <= TPlayer.transform.position.x) {
             Vector3 oposcale = transform.localScale;
             oposcale.x *= -1;
             transform.localScale = oposcale;
         }*/
        
     }//Flip


charplacement.jpg (17.2 kB)
avatar image highpockets Silentones · Mar 20, 2019 at 07:22 AM 0
Share

Ok, you want to send me a zip of your project??

If the enemy x scale is 5 when he starts out and you know he faces left, it should be as simple as this (I reintroduced your faceLeft variable as a float):

 float faceLeft;
 
 void Start(){
 faceLeft = enemy.transform.localScale.x;
 }
 
 void Update(){
 if(enemy.transform.position.x >= player.transform.position.x){
 enemy.transform.localScale.x = faceLeft;
 }else
 {
 enemy.transform.localScale.x = -faceLeft;
 }
 }

Show more comments

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

104 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

Related Questions

How to make unit priority targeting system? 2 Answers

Lock character's LocalScale during Idle animation 1 Answer

I just want to flip an enemy baed on their velocity 1 Answer

other enemies attack when they see one of them be killed by the player 2 Answers

Targeting system HELP please 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