At Official tutorial video and script. How can I make enemy interact also with Wall like player? So I want to add logic when enemy meet wall, enemy can damage it and destroy it. But OnCantMove<T> only receive one type? How should I revise?
You can change MovingScript in this line ( not tested, maybe have some sintax errors ): protected abstract void OnCantMove <T> (T component, YourType newComponent) where T : Component; or you can add another event : protected abstract void OnCantMoveEnemy <T> (T component, YourType newComponent) where T : Component;
I did like this, Seems not work... Enemy.cs Code (CSharp): using UnityEngine; using System.Collections; public class Enemy : MovingObject { public int playerDamage; private Animator animator; private Transform target; private bool skipMove; public AudioClip enemyAttack1; public AudioClip enemyAttack2; protected override void Start () { GameManager.instance.AddEnemyToList(this); animator = GetComponent<Animator>(); target = GameObject.FindGameObjectWithTag("Player").transform; base.Start(); } protected override void AttemptMove<T> (int xDir, int yDir){ if(skipMove){ skipMove = false; return; } base.AttemptMove<T>(xDir, yDir); skipMove = true; } public void MoveEnemy(){ int xDir = 0; int yDir = 0; if((Mathf.Abs(target.position.x - transform.position.x) > float.Epsilon)){ xDir = target.position.x > transform.position.x ? 1 : -1; } if(Mathf.Abs(target.position.y - transform.position.y) > float.Epsilon){ yDir = target.position.y > transform.position.y ? 1 : -1; } Debug.Log("ydir is "+yDir+" / xdir is "+xDir); AttemptMove<Player>(xDir, yDir); AttemptMove<Wall>(xDir, yDir); } protected override void OnCantMove<T>(T component){ Player hitPlayer = component as Player; animator.SetTrigger("enemyAttack"); hitPlayer.LoseFood(playerDamage); SoundManager.instance.RandomizeSfx(enemyAttack1, enemyAttack2); } protected override void OnCantMoveEnemy<T>(T component){ Wall hitWall = component as Wall; Debug.Log("hit wall is "+hitWall.name); } } MovingObject.cs Code (CSharp): using UnityEngine; using System.Collections; public abstract class MovingObject : MonoBehaviour { public float moveTime = 0.1f; public LayerMask blockingLayer; private BoxCollider2D boxCollider; private Rigidbody2D rb2D; private float inverseMoveTime; // Use this for initialization protected virtual void Start () { boxCollider = GetComponent<BoxCollider2D>(); rb2D = GetComponent<Rigidbody2D>(); inverseMoveTime = 1f / moveTime; } protected bool Move(int xDir, int yDir, out RaycastHit2D hit){ Vector2 start = transform.position; Vector2 end = start+new Vector2(xDir, yDir); boxCollider.enabled = false; hit = Physics2D.Linecast(start, end, blockingLayer); boxCollider.enabled = true; if(hit.transform == null){ StartCoroutine(SmoothMovement(end)); return true; } return false; } protected IEnumerator SmoothMovement(Vector3 end){ float sqrRemainingDistance = (transform.position-end).sqrMagnitude; while(sqrRemainingDistance > float.Epsilon){ Vector3 newPosition = Vector3.MoveTowards(rb2D.position, end, inverseMoveTime * Time.deltaTime); rb2D.MovePosition(newPosition); sqrRemainingDistance = (transform.position-end).sqrMagnitude; yield return null; } } protected virtual void AttemptMove<T> (int xDir, int yDir) where T : Component{ RaycastHit2D hit; bool canMove = Move (xDir, yDir, out hit); if(hit.transform == null) return; T hitComponent = hit.transform.GetComponent<T>(); if(!canMove && hitComponent != null){ OnCantMove(hitComponent); if(hitComponent is Wall) OnCantMoveEnemy(hitComponent); } } protected abstract void OnCantMove<T> (T component) where T : Component; protected abstract void OnCantMoveEnemy <T> (T component) where T : Component; }
But what not work? What errors that did print? What line in player should do the same in enemy? It's looks right to me. =/
Is entering in this if statement? Moving.cs Code (CSharp): if(!canMove && hitComponent != null){ OnCantMove(hitComponent); if(hitComponent is Wall) OnCantMoveEnemy(hitComponent); }
And about your Code (CSharp): if(hitComponent is Wall) OnCantMoveEnemy(hitComponent); What kind of hitComponent is ?
Anyway, I solved on my way. I revised function. Code (CSharp): protected virtual void AttemptMove(int xDir, int yDir){ RaycastHit2D hit; bool canMove = Move (xDir, yDir, out hit); if(hit.transform == null) return; GameObject hitComponent = hit.transform.gameObject; if(!canMove && hitComponent != null){ //OnCantMove(hitComponent); Debug.Log("hit object name is "+hitComponent.name); if(hitComponent.GetComponent<Wall>() != null){ Component hitCom = hitComponent.GetComponent<Wall>(); OnCantMove(hitCom); } else if(hitComponent.GetComponent<Player>() != null){ Component hitCom2 = hitComponent.GetComponent<Player>(); OnCantMove(hitCom2); } } }