Question by
TheBigStudio · Sep 30, 2020 at 05:15 PM ·
scripting problemmovementenemy ai
Photon 2 Enemy AI Doesn't move?
Hello Guys, I have a enemy AI Script that moves randomly on my 2D map. But When i tried to get my script adapted to photon i failed because none of the enemies moving. They just wait on the map. Because I am new to photon i couldnt understand where i did mistake. Could you guys help me please? Thanks in advance...
Here is my Enemy AI Multi Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
public class EnemyAI_Multi : MonoBehaviourPunCallbacks
{
public float speed;
private Transform moveSpots = null;
private int randomSpot;
private float waitTime;
public float startWaitTime;
public float direction;
private EnemySpawner enemySpawner;
private GameHandler gameHandler;
public bool isScriptActive = true;
private AudioSource audioSource;
public float minX, maxX, minY, maxY;
private PhotonView pW;
private void Awake()
{
pW = GetComponent<PhotonView>();
audioSource = GetComponent<AudioSource>();
gameObject.GetComponent<EnemyAI_Multi>().enabled = true;
enemySpawner = GameObject.Find("SpawnEnemy").GetComponent<EnemySpawner>();
gameHandler = GameObject.Find("GameHandler").GetComponent<GameHandler>();
}
void Start()
{
waitTime = startWaitTime;
pW.RPC("setPosition", RpcTarget.AllBuffered);
}
void Update()
{
pW.RPC("moveEnemy", RpcTarget.AllBuffered);
}
private void Flip(float direction)
{
Vector3 scale = transform.localScale;
scale.x = direction;
transform.localScale = scale;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (isScriptActive)
{
if (collision.gameObject.tag == "EnemyFish")
{
pW.RPC("setPosition", RpcTarget.AllBuffered);
}
}
if (collision.gameObject.tag == ("FishPlayer") && transform.localScale.z < collision.gameObject.transform.localScale.z)
{
isScriptActive = false;
audioSource.Play();
gameObject.GetComponent<SpriteRenderer>().enabled = false;
gameObject.GetComponent<Rigidbody2D>().gravityScale = 0.1f;
gameObject.GetComponent<Animator>().enabled = false;
gameObject.GetComponent<BoxCollider2D>().enabled = false;
gameHandler.fishCounter += 1;
enemySpawner.fishCount -= 1;
Destroy(gameObject, 1f);
}
}
private void OnCollisionStay2D(Collision2D collision)
{
if (isScriptActive)
{
if (collision.gameObject.tag == "EnemyFish")
{
pW.RPC("setPosition", RpcTarget.AllBuffered);
}
if (collision.gameObject.tag == "MapBorders")
{
pW.RPC("setPosition", RpcTarget.AllBuffered);
}
}
}
[PunRPC]
private void moveEnemy()
{
if (isScriptActive)
{
pW.RPC("Move", RpcTarget.AllBuffered);
if (Vector2.Distance(transform.position, moveSpots.position) < 0.2f)
{
if (waitTime <= 0)
{
pW.RPC("setPosition", RpcTarget.AllBuffered);
waitTime = startWaitTime;
}
else
{
waitTime -= Time.deltaTime;
}
}
if (moveSpots.position.x < transform.position.x)
{
Flip(direction);
}
else if (moveSpots.position.x > transform.position.x)
{
Flip(-direction);
}
}
}
[PunRPC]
private void setPosition()
{
moveSpots.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
}
[PunRPC]
private void Move()
{
transform.position = Vector2.MoveTowards(transform.position, moveSpots.position, speed * Time.deltaTime);
}
}
Comment