- Home /
Question by
Evil-ProHack · Jul 24, 2020 at 11:07 AM ·
animationupdatemovement scriptstate-machinepositions
Script works with one gameObject but not works with other (update position)
I have a script that throw the arms to the player but only works with the right arm in the case of the left not work i do not know why with debbuging also cant see the error (In this case is not an animation problem without animation still not working)
public class Boss2ArmAttackBehaviour : StateMachineBehaviour
{
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.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);
Debug.Log("this is update");
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);
Debug.Log("this is update");
timer -= Time.deltaTime;
timeToRecalculate -= Time.deltaTime;
Debug.Log("Brazo en movimiento l");
// direction = player.transform.position - rightArm.transform.position;
direction.Normalize();
leftArm.transform.position = leftArm.transform.position + direction * armSpeed;
}
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Game broken after Unity 4.5.1 update 1 Answer
Should we check animation condition in Update or FixedUpdatE? 3 Answers
Parent and child repositioning 0 Answers
Handling Movement - Animations 1 Answer