Jump to content

Archived

This topic is now archived and is closed to further replies.

Evilhack

transform.position no se modifica en un caso pero en otro si

Recommended Posts

Estoy haciendo un juego de naves y un jefe tiene un ataque que lanza al jugador o su ala izquierda o su ala derecha. El caso es que en la ala derecha todo bien pero en la izquierda  no se mueve, he puesto unos Debug.Log    y se muestran correctamente pero la ala sigue sin moverse 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.PlayerLoop;

public class Boss2ArmAttackBehaviour : StateMachineBehaviour
{
  //  public bool isAtackingWithRightArm = false;
    Vector3 rightArmPos;
    Vector3 leftArmPos;


    public Transform leftArm;
    public Transform rightArm;
    public Transform rightArmB;
    public PlayerBehaviour player;
    public BossBehaviour2 boss;
    int rand;
    public float timer;
    public float minTime;
    public float maxTime;
    public float armSpeed;
    public Vector3 direction;
    public Animator animatorr;
    Transform rArm;
    Transform lArm;
    public Vector3 direction2;
    public  float timeToRecalculate;
    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        animatorr = animator;
        timer = Random.Range(minTime, maxTime);
        animator.SetFloat("timelife", timer);
        //boss = GameObject.Find("Bossu2RealVariant(Clone)").GetComponent<BossBehaviour2>();       
        boss = GameObject.Find("Bossu2RealVariant").GetComponent<BossBehaviour2>();
        player = GameObject.Find("nave").GetComponent<PlayerBehaviour>();
        rArm = GameObject.Find("spaceParts_020").GetComponent<Transform>();
        rightArm = GameObject.Find("RightArm").GetComponent<Transform>();
        rightArmPos = rightArm.localPosition;
        lArm = GameObject.Find("spaceParts_019").GetComponent<Transform>();
        leftArm = GameObject.Find("LeftArm").GetComponent<Transform>();
        leftArmPos = leftArm.localPosition;
        // animator.SetBool("isAttackingWithArms", true);
        float lifetime = 0.3f;
        //TODO metodo para los dos brazos 
        if (animator.GetBool("rightArm") == true)
        {
            StartRightArm();
            Debug.Log("r arm started");
        }
        if (animator.GetBool("leftArm") == true)
        {
        StartLeftArm();

            Debug.Log("l arm started");

        }
    }


    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
           CheckArm();
         

    }
 
    public void StartRightArm()
    {

      
        rightArm.localPosition = new Vector3(-1.655198f, -1.130143f, 0);
        Debug.Log(animatorr.GetParameter(0));
        //isAtackingWithRightArm = true;
        Debug.Log("posicion r" + rightArm.transform.position);
        Debug.Log("Espero que te llegue r");
        // animator.SetFloat("timelife", lifetime);
        rightArm.transform.position = rightArm.transform.position + direction * armSpeed;
        timeToRecalculate = Random.Range(2, 4);
        direction = player.transform.position - rightArm.transform.position;
        direction.Normalize();

    }
    public void StartLeftArm()
    {

      
        leftArm.localPosition = new Vector3(1.655198f, -1.130143f, 0);
        Debug.Log(animatorr.GetParameter(0));
        //isAtackingWithRightArm = true;
        Debug.Log("posicion l" + leftArm.transform.position);
        Debug.Log("Espero que te llegue l");
        // animatorr.SetFloat("timelife", lifetime);
        leftArm.transform.position = leftArm.transform.position + direction * armSpeed;
        timeToRecalculate = Random.Range(2, 4);
        direction = player.transform.position - leftArm.transform.position;
        direction.Normalize();

    }
    public void CheckArm()
    {
       // Debug.Log("timer" + timer + "tietorec" +timeToRecalculate);
        if (!PauseMenuBehaviour.isPaused && player.life >= 0)
        {
            if (timeToRecalculate <= 0)
            {

                if (animatorr.GetBool("rightArm"))
                {
                    direction2 = player.transform.position - rightArm.transform.position;
                    direction2.Normalize();
                    direction = direction2;
                    rightArm.transform.position = rightArm.transform.position + direction * armSpeed;
                    timeToRecalculate = Random.Range(2, 4);
                    Debug.Log("Direccion recalculada r");

                }
                else if (animatorr.GetBool("leftArm"))
                {
                    direction2 = player.transform.position - leftArm.transform.position;
                    direction2.Normalize();
                    direction = direction2;
                    leftArm.transform.position = leftArm.transform.position + direction * armSpeed;
                    timeToRecalculate = Random.Range(2, 4);
                    Debug.Log("Direccion recalculada l");

                }

            }

            if (timer <= 0)
            {
                if (animatorr.GetBool("rightArm"))
                {
                    Debug.Log("He esperado el timer r");
                    //    Destroy(rightArm.gameObject, lifetime);
                    // isAtackingWithRightArm = false;
                    rightArm.transform.rotation.Set(0, 0, 0, 0);
                    rightArm.localPosition = new Vector3(-1.655198f, -1.130143f, 0);
                    Debug.Log("Brazo r en su sitio");
                    animatorr.SetBool("rightArm", false);
                    animatorr.ResetTrigger("isAttackingWithRightArm");
                    if (animatorr.GetBool("phase2Trrigered") == false)
                    {

                        animatorr.SetTrigger("idle");
                        //                        animatorr.SetTrigger("phase2");
                        //                      animatorr.SetBool("phase2Trrigered", true);
                    }
                    else
                    {
                        animatorr.SetTrigger("phase2Idle");
                    }
                    //   rightArm.localPosition = new Vector3(-1.655198f, -1.130143f, 0);

                }
                else if (animatorr.GetBool("leftArm"))
                {
                    Debug.Log("He esperado el timer l");
                    //    Destroy(rightArm.gameObject, lifetime);
                    // isAtackingWithRightArm = false;
//                    leftArm.transform.rotation.Set(0, 0, 0, 0);
                    leftArm.transform.rotation = new Quaternion(0, 0, 0, 0);
                    leftArm.localPosition = new Vector3(1.655198f, -1.130143f, 0);
                    Debug.Log("Brazo l en su sitio");
                    animatorr.SetBool("leftArm", false);
                    animatorr.ResetTrigger("isAttackingWithRightArm");
                    if (animatorr.GetBool("phase2Trrigered") == false)
                    {

                        animatorr.SetTrigger("idle");
                        //                        animatorr.SetTrigger("phase2");
                        //                      animatorr.SetBool("phase2Trrigered", true);
                    }
                    else
                    {
                        animatorr.SetTrigger("phase2Idle");
                    }
                    //   rightArm.localPosition = new Vector3(-1.655198f, -1.130143f, 0);

                }

            }else
            {
                if (animatorr.GetBool("rightArm"))
                {
                    //TODO 
                    //rArm.transform.Rotate(Vector3.back * armSpeed * Time.deltaTime);
                    //  rArm.transform.rotation = new Quaternion(rArm.transform.rotation.x,rArm.transform.rotation.y,rArm.transform.rotation.z + 1,100);

                    //rightArm.transform.Translate(Vector3.down * armSpeed * Time.deltaTime);
                
                    timer -= Time.deltaTime;
                    timeToRecalculate -= Time.deltaTime;
                    Debug.Log("Brazo en movimiento r");
                    // direction = player.transform.position - rightArm.transform.position;
                    direction.Normalize();
                    rightArm.transform.position = rightArm.transform.position + direction * armSpeed;
                    Debug.Log("");

                }
                else if (animatorr.GetBool("leftArm"))
                {
                    //TODO 
                    //lArm.transform.Rotate(Vector3.back * armSpeed * Time.deltaTime);
                    //  rArm.transform.rotation = new Quaternion(rArm.transform.rotation.x,rArm.transform.rotation.y,rArm.transform.rotation.z + 1,100);

                    //rightArm.transform.Translate(Vector3.down * armSpeed * Time.deltaTime);
                 
                    timer -= Time.deltaTime;
                    timeToRecalculate -= Time.deltaTime;
                    Debug.Log("Brazo en movimiento l");
                    Debug.Log(leftArm.name + "  " + direction + "" + armSpeed);
                    // direction = player.transform.position - rightArm.transform.position;
                    direction.Normalize();
                   // leftArm.Translate(leftArm.transform.position + direction * armSpeed);
                    leftArm.gameObject.transform.position = leftArm.transform.position + direction * armSpeed;

                }

            }
        }
    }
}

 

Share this post


Link to post
Share on other sites

×
×
  • Create New...