This shows you the differences between two versions of the page.
pjv:laboratoare:2022:03 [2022/11/09 12:29] alexandru.gradinaru |
pjv:laboratoare:2022:03 [2022/11/10 15:05] (current) alexandru.gradinaru |
||
---|---|---|---|
Line 230: | Line 230: | ||
</code> | </code> | ||
- | O problema in activitatea agentilor este detectarea player-ului, in sensul de referinta. Astfel, avem mai multe variante: | ||
- | * putem cauta un obiect dupa tag | ||
- | * intr-o variabila target putem referentia direct player-ul (dar asta inseamna ca la fiecare agent trebuie mapat) | ||
- | * putem folosi un singleton in care se tine referentiaza playerul si poate fi accesat de oriunde | ||
- | <code> | ||
- | public class PlayerManager : MonoBehaviour { | ||
- | |||
- | public static PlayerManager instance; | ||
- | public GameObject player; | ||
- | | ||
- | void Awake() | ||
- | { | ||
- | instance = this; | ||
- | } | ||
- | |||
- | } | ||
- | </code> | ||
- | |||
- | Folosind varianta simpla cu singleton, putem lua pozitia player-ului de inters, similar cu laboratorul precedent: | ||
- | |||
- | <code> | ||
- | target = PlayerManager.instance.player.transform; | ||
- | </code> | ||
- | |||
- | Astfel, putem efectua usor operatii care tin de player - de exemplu putem orienta inamicii sau un npc cu fata catre player, in momentul unei interactiuni. | ||
- | |||
- | <code> | ||
- | |||
- | //Roteste cu 90 grade | ||
- | void RotateN() { | ||
- | Vector3 currentRotation = transform.rotation; | ||
- | Vector3 wantedRotation = currentRotation * Quaternion.AngleAxis(-90, Vector3.up); | ||
- | transform.rotation = Quaternion.Slerp(currentRotation, wantedRotation, Time.deltaTime * rotationSpeed); | ||
- | } | ||
- | //Roteste inamicul cu fata catre player | ||
- | void FaceTarget () | ||
- | { | ||
- | Vector3 direction = (target.position - transform.position).normalized; | ||
- | Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z)); | ||
- | transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f); | ||
- | } | ||
- | </code> | ||
=== Miscarea personajului la o destinatie point-and-click === | === Miscarea personajului la o destinatie point-and-click === |