Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Cobradabest · Jan 07, 2013 at 08:31 PM · c#transformpositionbooleans

How do I check if an Object isn't moving? (C#)

I'm currently making the AI for my game, or the prototype AI, and I'm using the mechanim animator to animate it, so I'm making a boolean to check if the opponent is moving or not, so the animator will know what animation to play.

I'm having a problem with that, I'm checking if the opponent isn't moving by using a "previous position" value and the print function to check if it's working.

It isn't, even when I get to print both positions, and it's exactly the same, it still recognises it as moving.

Here's the code:

 void Update () 
     {
         if (myTransform.position == previousPosition)
         {
             moving = false;
             print("IT ISNAE MOVIN!");
         }
         else
         {
             //moving = true;
             print("IT'S ALIVE!" + transform.position.ToString() + previousPosition.ToString());
         }
         Debug.DrawLine(target.transform.position, transform.position, Color.red);
         
         //Look at target
         if (gameObject.tag == "Kruncanite")
         {
             myTransform.localRotation = Quaternion.Slerp(myTransform.rotation, 
             Quaternion.LookRotation(new Vector3(target.position.x, 0, target.position.z) - 
             new Vector3 (myTransform.position.x, 0, myTransform.position.z)),    rotationSpeed * Time.deltaTime);
         }
         else
         {
         myTransform.localRotation = Quaternion.Slerp(myTransform.rotation, 
             Quaternion.LookRotation(target.position - 
             myTransform.position),    rotationSpeed * Time.deltaTime);
         }
         
         //Move towards target
         
         if(Vector3.Distance(transform.position,target.position) > followDistance)
         {
             myTransform.position += Vector3.forward * moveSpeed * Time.deltaTime;
         }
         
         if (gameObject.tag == "Kruncanite")
         {
           kruncaniteAnimation.SetBool("Moving", moving);
         }
         previousPosition = myTransform.position;
     }

What am I doing wrong?

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 Seth-Bergman · Jan 07, 2013 at 08:48 PM

Well, you are changing the position right BEFORE the check:

     myTransform.position += Vector3.forward * moveSpeed * Time.deltaTime;

Unless "moveSpeed" equals zero, this changes your position ALWAYS;

    if (myTransform.position == previousPosition) 


is therefore always not met. Even if you are at the same position as the target, using this method will still move FORWARD.

You can add somethiong like this:

 float followDistance = 1.0;

 if(Vector3.Distance(transform.position,target.position) > followDistance)  //just add this line to your code 
    myTransform.position += Vector3.forward * moveSpeed * Time.deltaTime;
 ...etc

this way, once you are close enough, you stop moving

the point is you need to update the movement code so that the character actually CAN stop moving!

Comment
Add comment · Show 8 · 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 Cobradabest · Jan 07, 2013 at 08:56 PM 0
Share

Alright, I've moved it to the top, and it's flagging it as false sometimes, but when when the enemy is at a standstill, it keeps switching from true to false.

The code you provided me to move the object doesn't work as well as what I had, most of the time, the enemy would just move in one direction and only sometimes change, either that, or it would stay on the same spot and keep switch between 2 directions.

avatar image Seth-Bergman · Jan 07, 2013 at 09:07 PM 0
Share

yeah, sorry, I didn't really think that would work as written , it's more of an example..

here's one that will actually work :) :::::::::

 float followDistance = 1.0;

 if(Vector3.Distance(transform.position,target.position) < followDistance)  //just add this line to your code 
   myTransform.position += Vector3.forward * moveSpeed * Time.deltaTime;
 ...etc

this way, if you're close enough, you don't move..

I'll edit my answer, give me a few..

avatar image Cobradabest · Jan 07, 2013 at 10:49 PM 0
Share

Okay, I've actually added the character I want to make an A.I. (I tested it on boxes before), but they don't even move or turn when I use the script, do you know why that might be?

avatar image Seth-Bergman · Jan 08, 2013 at 12:00 AM 0
Share

Sorry I had < ins$$anonymous$$d of >

It should work now (fixed the answer)

avatar image Cobradabest · Jan 08, 2013 at 12:09 AM 0
Share

Now I'm having another problem, the character keeps moving to the left, the updated code is on the original question.

Show more comments

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make the crosshair go closer/get bigger if an item/wall is close to you? 2 Answers

C# GUI.Button Transform.Position 1 Answer

How to make cubes between 2 point and stick them together? 0 Answers

Simultaneously moving GameObjects using C# script. 3 Answers

My AI keeps flickering between stopping and moving. 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