• 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 c2egroeg · Apr 30, 2018 at 03:58 AM · 2d-platformersprite rendererflipping

Hi, just a small question. Why isn't my enemy flipping? I know I'm doing this right because my player if flipping, but what about my enemy. I'm getting really frustrated here.

@echoespf

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class EnemyScript : MonoBehaviour {

 public float delta = 1.5f;
 public float speed = 2.0f;
 private Vector2 startPos;

 public GameObject pelletPrefab;
 public GameObject negPelletPrefab;

 public float rayDist;

 public float fireRate = 0.5f;
 private float nextFire = 0.0f;

 public int enemyHealth;

 public bool facingRight;

 public SpriteRenderer enemySprite;

 // Use this for initialization
 void Start () {

     startPos = transform.position;
     
 }
 
 // Update is called once per frame
 void Update () {


     Vector2 rayStartRight = transform.position + new Vector3 (0.5f, 0, 0);
     Vector2 rayStartLeft = transform.position + new Vector3 (-0.5f, 0, 0);

     Vector3 v = startPos;
     v.x += delta * Mathf.Sin (Time.time * speed);
     transform.position = v;

     if (v.x > 0 && !facingRight) {
         enemySprite.flipX = false;
     } else if (v.x < 0 && facingRight) {
         enemySprite.flipX = true;
     } else {
         enemySprite.flipX = false;
     }

     Vector2 rayDirRight = new Vector2 (1, 0);
     Vector2 rayDirLeft = new Vector2 (-1, 0);

     Debug.DrawRay (rayStartRight, rayDist * rayDirRight, Color.red);
     Debug.DrawRay (rayStartLeft, rayDist * rayDirLeft, Color.blue);

     RaycastHit2D hitRight = Physics2D.Raycast (rayStartRight, rayDirRight, rayDist);
     RaycastHit2D hitLeft = Physics2D.Raycast (rayStartLeft, rayDirLeft, rayDist);

     /*Physics2D.Raycast (rayStart, rayDirRight, out hitRight, rayDist);
     Physics2D.Raycast (rayStart, rayDirLeft, out hitLeft, rayDist);*/

     if (hitRight.collider != null) {
         if (hitRight.collider.tag == "Player") {
             if (Time.time >= nextFire) {
                 Instantiate (pelletPrefab, transform.position, Quaternion.identity);
                 nextFire = Time.time + fireRate;
             }
         }
         //Debug.Log (hitRight.collider.gameObject);
     }

     if (hitLeft.collider != null) {
         if (hitLeft.collider.tag == "Player") {
             if (Time.time >= nextFire) {
                 Instantiate (negPelletPrefab, transform.position + new Vector3(-0.8f,0,0), Quaternion.identity);
                 nextFire = Time.time + fireRate;
             }
         }
     }
     
 }

 void OnCollisionEnter2D (Collision2D other){
     if (other.gameObject.tag == "PlayerBullet") {
         enemyHealth--;
         Destroy (other.gameObject);
         Debug.Log ("I hit");
         if (enemyHealth <= 0) {
             Destroy (gameObject);
         }
     } /*else {
         enemyHealth--;
     }*/
     Debug.Log ("I hit");
 }

}

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.

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

85 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

Related Questions

Would love some advice, based on a reply of yours about 2D sprite flipping. (U5.3) 0 Answers

How do you change a sprite's sorting layer in C#? 5 Answers

Flip Player, Not the Player's Child. 3 Answers

(2D sidescrolling platformer) Flipping my character right or left depending on the mouse? 2 Answers

How to monitor the movment speed of a 2D sprite 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges