35 lines
723 B
C#
Raw Permalink Normal View History

2023-05-21 17:47:39 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float maxSpeed;
private Rigidbody2D rb;
private CapsuleCollider2D cc;
private SpriteRenderer sr;
void Start()
{
rb = GetComponent<Rigidbody2D>();
cc = GetComponent<CapsuleCollider2D>();
sr = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void FixedUpdate()
{
rb.velocity = new Vector2(maxSpeed, rb.velocity.y);
}
void OnTriggerEnter2D(Collider2D cc)
{
if (!cc.gameObject.CompareTag("Player"))
{
maxSpeed = -maxSpeed;
sr.flipX = !sr.flipX;
}
}
}