Mi juego es un RTS 2D, y me preguntaba si alguien conocía un buen tutorial para Unity, o si alguien versado en la sintaxis podría decirme qué podría haber hecho mal.
Entonces, tengo mi objeto de cámara y mi objeto de jugador, ambos etiquetados. El objeto del jugador solo tiene un sprite y está configurado como rigidbody. El guión es el siguiente:
using UnityEngine; using System.Collections; public class AIsciript : MonoBehaviour { private bool thisIsPlayer = true; private GameObject objPlayer; private GameObject objCamera; //input variables (variables used to process and handle input) private Vector3 inputRotation; private Vector3 inputMovement; //identity variables (variables specific to the game object) public float moveSpeed = 100f; // calculation variables (variables used for calculation) private Vector3 tempVector; private Vector3 tempVector2; // Use this for initialization void Start() { objPlayer = (GameObject)GameObject.FindWithTag("Player"); objCamera = (GameObject)GameObject.FindWithTag("MainCamera"); if (gameObject.tag == "Player") { thisIsPlayer = true; } } // Update is called once per frame void Update() { FindInput(); ProcessMovement(); if (thisIsPlayer == true) { HandleCamera(); } } void FindInput() { if (thisIsPlayer == true) { FindPlayerInput(); } else { FindAIInput(); } } void FindPlayerInput() { //find vector to move inputMovement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); //find vector to the mouse tempVector2 = new Vector3(Screen.width * 0.5f, 0, Screen.height * 0.5f); // the position of the middle of the screen tempVector = Input.mousePosition; // find the position of the mouse on screen tempVector.z = tempVector.y; tempVector.y = 0; Debug.Log(tempVector); inputRotation = tempVector - tempVector2; } void FindAIInput() { } void ProcessMovement() { rigidbody.AddForce(inputMovement.normalized * moveSpeed * Time.deltaTime); objPlayer.transform.rotation = Quaternion.LookRotation(inputRotation); objPlayer.transform.eulerAngles = new Vector3(0, transform.eulerAngles.y + 180, 0); objPlayer.transform.position = new Vector3(transform.position.x, 0, transform.position.z); } void HandleCamera() { objCamera.transform.position = new Vector3(transform.position.x, 15, transform.position.z); objCamera.transform.eulerAngles = new Vector3(90, 0, 0); } } Pensé que publicaría el código por si acaso, pero creo que probablemente no sea el problema, ya que traté de forzarlo a moverse en Start() y no sucedió nada.
No debería usar todos esos controles para thisIsPlayer. Debe tener clases separadas para la entidad jugadora y las entidades no jugadoras.
Las variables públicas se exponen en el editor y se serializan con la entidad cuando se guarda el nivel. Esto podría significar que moveSpeed no está configurado actualmente en lo que se inicializó en esta clase.
No debe agregar fuerza a un cuerpo rígido en el método Actualizar. Hay un método FixedUpdate que se usa para aplicar la física. Esto se debe a que Update se llama una vez por fotograma, sin importar la velocidad de fotogramas, y FixedUpdate solo se llama a intervalos específicos, por lo que las fuerzas físicas no se ven afectadas por la velocidad de fotogramas.
Además, no debe intentar aplicar una fuerza y establecer la posición de la transformación del mismo objeto. Sucederán cosas extrañas.
Si ingresa a la Tienda de activos de Unity (disponible en el menú Ventana dentro de Unity), hay una sección llamada "Proyectos completos" que contiene algunos tutoriales gratuitos. No recuerdo cuál de ellos está escrito en C#, pero incluso los de JavaScript te darán algunas ideas sobre cómo estructurar el proyecto.
No se si entendí bien: tu problema es que no lo mueve la IA?
si esto es así, un problema podría ser que inicialice su
private bool thisIsPlayer = true;con verdadero, pero no puedo ver ninguna condición que lo establezca en falso (ingrese al modo ai)
solo mis 2 centavos :)