1 year ago
#200525
Fadi Walhab
GetComponent<Interface>.method doesnt work
I am using Unity and C#
Hey Guys, i have a little problem:
If space is pressed i want another class to execute a method(you'll understand with the code).
First Class: everything working except of:
collider.GetComponent<NPCController>()?.Interact();
Full Method:
void Interact()
{
var facingPos = new Vector3(animator.GetFloat("moveX"), animator.GetFloat("moveY"));
var interactPos = transform.position + facingPos;
var collider = Physics2D.OverlapCircle(interactPos, 0.2f, InteractableLayer);
if(collider != null)
{
collider.GetComponent<NPCController>()?.Interact();
Debug.Log("Interactmethod working");
}
}
Other Classes I use are NPCController that implements InteractableObjects with the Method Interact():
public void Interact()
{
Debug.Log("Finished the path");
}
and InteractableObjects interface:
public interface InteractableObjects
public void Interact();
Full Player Controller class for everyone who wants to know:
using System.Collections;
public class PlayerController : MonoBehaviour { public float moveSpeed; public LayerMask SolidObjectsLayer;
public float moveSpeed;
public LayerMask SolidObjectsLayer;
public LayerMask InteractableLayer;
public bool isMoving;
private Vector2 input;
private Animator animator;
private void Awake()
{
animator = GetComponent<Animator>();
}
private void Update()
{
if(!isMoving)
{
//INPUT NOCHMAL ANSCHAUEN
input.x = Input.GetAxisRaw("Horizontal"); //GetAxisRaw heisst, dass es immer 1 oder -1 ist.
input.y = Input.GetAxisRaw("Vertical");
//Keine Diagonal Bewegung
if(input.x != 0)
{
input.y = 0;
}
if(input != Vector2.zero)
{
animator.SetFloat("moveX", input.x);
animator.SetFloat("moveY", input.y);
var newPos = transform.position; //neue Position veraendert sich nach Eingabewerten.
newPos.x += input.x;
newPos.y += input.y;
if(IsWalkable(newPos))//Kann er Laufen oder ist ein Object im Weg wird mit dem bool Wert isWalkable geprueft
StartCoroutine(Move(newPos));
}
}
animator.SetBool("isMoving", isMoving);
if(Input.GetKeyDown(KeyCode.Space))
{
Interact();
Debug.Log("KeyListener Working");
}
}
void Interact()
{
var facingPos = new Vector3(animator.GetFloat("moveX"), animator.GetFloat("moveY"));
var interactPos = transform.position + facingPos;
var collider = Physics2D.OverlapCircle(interactPos, 0.2f, InteractableLayer);
if(collider != null)
{
collider.GetComponent<NPCController>()?.Interact();
Debug.Log("Methoder Aufgerufen im InteractableObjects interface");
}
}
IEnumerator Move(Vector3 newPos)
{
isMoving = true;
while((newPos - transform.position).sqrMagnitude > Mathf.Epsilon) //Wenn der Unterschied zwischen Target und AktuellPos ist gross
{
transform.position = Vector3.MoveTowards(transform.position, newPos, moveSpeed * Time.deltaTime); //
yield return null; //Ausfuehrung stoppen
}
transform.position = newPos;
isMoving = false;
}
private bool IsWalkable(Vector3 newPos)//Kann man zu dem Vector newPos laufen?
{
if(Physics2D.OverlapCircle(newPos, 0.2f, SolidObjectsLayer | InteractableLayer) != null)// wenn die Zielposition zum solidObjectslayer gehoert dann nicht
{
return false; //kann nicht laufen
}
return true; //kann laufen. Weg ist frei
}
}
c#
unity-game-engine
interface
implements
0 Answers
Your Answer