Skip to content
Permalink
d864e80fc3
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
55 lines (45 sloc) 1.26 KB
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyPatrol : MonoBehaviour
{
public bool moveRight;
public static float speed;
public GameObject Ghost;
public SpriteRenderer mySpriteRenderer;
void Start()
{
speed = (Random.Range(1f, 1.5f));
Rigidbody2D rigidbody = GetComponent<Rigidbody2D>();
rigidbody.velocity = transform.right * speed;
mySpriteRenderer = GetComponent<SpriteRenderer>();
}
void Update()
{
if (moveRight)
{
transform.Translate(2 * Time.deltaTime * speed, 0, 0);
if (mySpriteRenderer != null)
{
// flip the sprite
mySpriteRenderer.flipX = true;
}
}
if (moveRight == false)
{
transform.Translate(2 * Time.deltaTime * -(speed*2), 0, 0);
mySpriteRenderer.flipX = false;
}
}
void OnTriggerEnter2D(Collider2D other) //Pickups
{
if (other.gameObject.CompareTag("turn")) //If you got the key
{
moveRight = false;
}
if (other.gameObject.CompareTag("turnback")) //go right
{
moveRight = true;
}
}
}