- Home /
Stop object from moving
I have a script attached to my enemy robot where he follows me and then attacks when he gets close enough. The script works fine, though when he is attacking he's still moving forward (z-axis), so I need him to stop moving and rotating when attacking. Here is the script:
using UnityEngine;
using System.Collections;
public class CharacterPursueYbot : MonoBehaviour {
public Transform player;
static Animator anim;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void Update ()
{
if(Vector3.Distance(player.position, this.transform.position) < 100)
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp (this.transform.rotation,
Quaternion.LookRotation (direction), 0.1f);
anim.SetBool ("isIdle", false);
if (direction.magnitude > 2)
{
this.transform.Translate (0, 0, 0.05f);
anim.SetBool ("injuredWalk", true);
anim.SetBool ("bash", false);
}
else
{
// STOP MOVING AND ROTATING STATEMENT HERE
anim.SetBool ("bash", true);
anim.SetBool ("injuredWalk", false);
}
}
else
{
anim.SetBool ("isIdle", true);
anim.SetBool ("injuredWalk", false);
anim.SetBool ("bash", false);
}
}
}
I need him to stop moving and rotating in my direction when;
// STOP MOVING AND ROTATING STATEMENT HERE anim.SetBool ("bash", true); anim.SetBool ("injuredWalk", false);
So basically, when he is attacking i need him to stop moving and rotating.
How should I go about this?
THANKS
Answer by juicyz · Jan 09, 2017 at 07:31 PM
You are going to want to do something like this. I don't know exactly where you want the if statement but if you check if he is currently bashing then he won't update. You likely want to wrap the rotation logic in this if statement. You also would need to negate it but I think you get the point hopefully :) if not, comment back.
if (anim.GetBool("bash"))
{
// Basically do nothing
return;
}
Thank you, but I'm not that experienced in scripting so how would my script look now?
This might work... I honestly would likely restructure the code but I don't know enough about the game to do so I'm guessing that this might work. $$anonymous$$eeping the same gameplay that your game currently has:
// Update is called once per frame
void Update ()
{
if(Vector3.Distance(player.position, this.transform.position) < 100)
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
if (anim.GetBool("bash")) {
this.transform.rotation = Quaternion.Slerp (this.transform.rotation,
Quaternion.LookRotation (direction), 0.1f);
}
anim.SetBool ("isIdle", false);
if (direction.magnitude > 2)
{
if (anim.GetBool("bash")) {
this.transform.Translate (0, 0, 0.05f);
}
anim.SetBool ("injuredWalk", true);
anim.SetBool ("bash", false);
}
else
{
// STOP $$anonymous$$OVING AND ROTATING STATE$$anonymous$$ENT HERE
anim.SetBool ("bash", true);
anim.SetBool ("injuredWalk", false);
}
}
else
{
anim.SetBool ("isIdle", true);
anim.SetBool ("injuredWalk", false);
anim.SetBool ("bash", false);
}
}