Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
AI sigue y gira hacia el jugador

Tengo un problema con mi script, quiero que mi enemigo me siga y gire hacia el jugador. mientras se mueve. Parece funcionar bien, pero cuando mi jugador gira 180 en y, mi enemigo parece retroceder mucho (su posición), y solo cuando mi jugador vuelve a su rotación normal, el enemigo parece volver. ¿Qué hice mal?

 public class EnemyTesting : MonoBehaviour { public GameObject player; public float speed = 1.5f; public float turnRate; private void Update() { Vector3 toTarget = player.transform.position - transform.position; float angleToTarget = Vector3.Angle(transform.forward, toTarget); Vector3 turnAxis = Vector3.Cross(transform.forward, toTarget); transform.RotateAround(transform.position, turnAxis, Time.deltaTime * turnRate * angleToTarget); transform.Translate(toTarget * speed * Time.deltaTime); }

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Por "retrocede" entiendo rotación, no posición. Creo que tu problema es con Vector3.Angle. Verifique en los documentos que la función devuelve el menor de los dos ángulos posibles entre los dos vectores. En el caso de que el ángulo sea mayor a 180, debes manejar eso en el código para hacer que tu enemigo gire en consecuencia.

Editar: creo que eso se produce por el espacio de rotación de la función de traducción. Establezca Space.World en los argumentos transform.Translate Pruebe esto:

 private void Update() { Vector3 toTarget = player.transform.position - transform.position; transform.LookAt(player.transform.position); transform.Translate(toTarget * speed * Time.deltaTime, Space.World); }
over 4 years ago · Santiago Trujillo Report

0

Podrías usar Quaternion.RotateTowards para rotar lentamente la dirección del enemigo hacia el jugador.

Este comportamiento también hace que el enemigo camine hacia su vector de avance, en lugar de directamente hacia el jugador.

 var towardsPlayer = player.transform.position - transform.position; transform.rotation = Quaternion.RotateTowards( transform.rotation, Quaternion.LookRotation(towardsPlayer), Time.deltaTime * turnRate ); transform.position += transform.forward * speed * Time.deltaTime;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!