Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Exuberant · Jan 09, 2017 at 11:37 AM · boolstop

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Exuberant · Jan 10, 2017 at 03:39 AM 0
Share

Thank you, but I'm not that experienced in scripting so how would my script look now?

avatar image juicyz Exuberant · Jan 10, 2017 at 04:10 AM 0
Share

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);
          }
      }

avatar image Exuberant juicyz · Jan 10, 2017 at 11:15 AM 0
Share

Thank you so much, it works fine

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

how to stop the player when gameover?? 1 Answer

Grounded bool acting weird 1 Answer

How to start animation after dialogue ends a.k.a currentLine == 3 1 Answer

Enemy ignoring script 1 Answer

my button dosent work, became a light switch instead of a normal button (like a bell) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges