Check on movement/speed helppls

How do I do a IF Moving speed check from a set destination? I need to know if my guy is moving or stopped, but I’ve been googling tutorials all day, and they lead me all around the place, alot of legacy syntax. Or people giving just little bits of it, and not the full deal. I need to get my function going to call upon if speed is 0 or >. Doesn’t sound hard? Well it’s not easy to narrow down youtube searches.

Am I supposed to use this place or that answers place? Or is it either or?

First of all, post your code.
Secondly, couldn’t you use a bool variable that check if the player is moving or not? (or an integer if you need multiple options or speed checks other than moving or not moving).
You add it to your movement script, if you are moving then the bool Moving = true, else if you are not moving then the bool Moving = false and the script knows you aren’t moving.
Now, after you done that, you can do whatever you want by calling your bool variable and checking if it’s true or not.

. Here is my code: My if statements are working of the wrong thing, it cannot be on the wandertime. Stuff Marked //////// I want to go by a movement yes/no. I dont know about the if moving or not variable. Ive been searching all day.

using UnityEngine;
using UnityEngine.AI;
using System.Collections;
using System.Collections.Generic;
public class MoveHorse : MonoBehaviour {
        public float wanderRadius;
        public float wanderTimer;
        private Transform target;
        private NavMeshAgent agent;
        private float timer;
 

            agent = GetComponent<NavMeshAgent> ();
            timer = wanderTimer;
        }
     
        void Update () {

            timer += Time.deltaTime;
    
        if (timer >= wanderTimer) {
            Vector3 newPos = RandomNavSphere (transform.position, wanderRadius, -1);
        agent.SetDestination (newPos);
        timer = 0;
      
   ///////////////////////////////////////////////////////////////////   
            GameObject.Find("WBhorse").GetComponent<Animator>().SetBool("IsMove", true);
 
            }else{
                GameObject.Find("WBhorse").GetComponent<Animator>().SetBool("Idle", true);
            }
 //////////////////////////////////////////////////////////////////////////
/////////this should be running off movement yes or no

     
             
         
            }
        public static Vector3 RandomNavSphere(Vector3 origin, float dist, int layermask) {
            Vector3 randDirection = Random.insideUnitSphere * dist;
            randDirection += origin;
            NavMeshHit navHit;
            NavMesh.SamplePosition (randDirection, out navHit, dist, layermask);
            return navHit.position;
        }
 
}

Edit your post and use Code Tags too please : Using code tags properly .
If you need to highlight something, simply take that part from the script in example (by surrounding it with CODE tags) or add a comment to that part (// or /* */)
It makes the code easier to read for others and you will get much more help.

It is ready for help now.

not speed, velocity (speed in a direction)
https://docs.unity3d.com/ScriptReference/AI.NavMeshAgent-velocity.html

edit:
if you want the “speed” or magnitude of the vector you’ll need
https://docs.unity3d.com/ScriptReference/Vector3-magnitude.html
https://docs.unity3d.com/ScriptReference/Vector3-sqrMagnitude.html

I had this bit

//    if (WBhorse.velocity == Vector3.zero);
//////I did not include all the sample codes I have been already trying.

I’m just getting messed up with it. I think I am going to need a total example and back engineer it to get it. I’ve been on that unity site all day. I cannot find a pure example to take, that works, without a bunch of mess.

I figure it will be to call a function each frame or so, and then compare coordinates, and then when it stops being different, I know my guy is stopped. I’ve been trying code all day with it, but I don’t get how to place it all, and the advice I keep getting off youtube, etc. is all legacy stuff mixed in, and then the Unity Site only gives you small parcels, never complete everything together, so I have found this wild goose chase scenario to learn, though it is not lack of effort on my part.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
   public Transform other;
   public float closeDistance = 5.0F;
   void Update() {
       if (other) {
           Vector3 offset = other.position - transform.position;
           float sqrLen = offset.sqrMagnitude;
           if (sqrLen < closeDistance * closeDistance)
               print("The other transform is close to me!");
        
       }
   }
}

I get what this is doing. But not how to apply it in my code around what I want to do. I already investigate this snippet yesterday. I would need to check the other.positions before hand, record them in a variable, and then when the transform.position stops changing, that means, the variable stops altering with each call for the function, it is go time for my Idle animation. I’m stuck on the syntax level of things, and code placement.

I have already got that logic down. As said I did not include the code I was contemplating

//IsMoving = true;
        //     else IsMoving = false;

Was already in my code that I scrubbed out so you all could see what I had working functionally so far, but I do not know How to do it. And I cannot find any working examples from two days of searching, and this makes me wonder of the “mercantalist” attitude of the programming community.Should we not be totally universal, with no trade competitive advantage, of what you know or what others know? But this I feel the programming community has been always haughty in that way, even when twenty years ago, I was much further into this realm. That is to digress, but even so, I find example, and full example of coding very poor, and that is all people need, to simply back engineer. I get the logic of what needs to be done, all I need is an example, and if a person wanted to know an example of how to do what I know, which is not programming, but other variety of skills, I would show them. Once I have the example I can learn. Nothing is hard when you can get the full example of the code that works, but I find people are a bit tight about doing that, which I find silly. I am waiting for the example thankyou, not vague logic. And help goes around.

Your example :

public bool Moving; 
//Your code for moving and blahblah
bool Moving = true;
else
//In the else case, your player doesn't move so
bool Moving = false;

if (Moving == true) //or if (Moving) is fine too.
{
//Do whatever you want. You can play your animation or anything.
}
if (Moving == false) 
{
//Do whatever you want. You can play an idle animation or anything.
}

You probably never will. The point is to read the tutorials/examples/api/etc to ascertain and understand what they are doing that makes it work and apply that concept to your own situation.

If you’re trying to make a horse move with animations then the idea is to setup a Blend Tree of the animations in a Animation Controller, put that in an Animator Component on your horse and give it the values of input so it can play the correct animation. Then it’s just a matter of giving it good inputs so it can behave correctly.

That is all pretty standard stuff that the tutorials and example projects do cover, it just takes time to go over the material.

It’s not that we’re haughty, most people here are generally really helpful but we just like to see someone doing their due diligence and then posting when they are genuinely stuck, or if they have a specific question that they need an answer to.

It is easier to understand through example of code for me. I have addressed this in another post. I know why people want others to do tutorials and labour on, and it is not good, because I know clearly, absolutely that I follow better by going through example code that works, and your due diligence, are phantoms in your head, to not be rest assured by my own self judgement over my many years, of how I learn best, and thankyou for the example ihgyug, but to me it is thin, and this is the trouble, because all I need is working code that works with what I already posted, and I shall not debate the point anymore. I shall go and find better company, and not endure this delusion that “because you want me to labour through tutorials”, when there is no reason you cannot post full code that geared with what I posted, rather than sit and type out a long post on a forum, you instead could have done that, and it is a patronisation, and all men have this way about them, and it is a shame for the world. I will go off my own way now. End the thread.

Sure there is.

  • Because it’s your problem.
  • Tutorials already explain exactly how to do it.
  • You expect others to do it for you.

Since all three of these are true, it’s less than motivating for us to hand feed you a robust and custom solution.

If you don’t understand that, I’m sorry.

I expect logic.

I got some Indian guy helping me on youtube. I dont work well from these small examples. I am already aware of the Unity site. I find it hard to follow in its snippet type fashion. If it is not a full code example I don’t want to know. I have addressed this in a youtube video on my channel here: https://www.youtube.com/channel/UC3ofVytaja5jLh9eBB_03ZA

I am not going to use this site for help anymore. Not to be a crybaby or anything lol, its just I mean what I say on my youtube. Love the unity program though.

It’ll be virtually impossible to get every piece of code you want for your game on the internet, completely.
That’s why people suggest that you learn. Perhaps that seems silly to you, that why can’t others just answer everything you ask – I find on the forums, there’s a mixture of a solution and a direction offered, with responses. :slight_smile:
The answers to help you were posted here, though, by the way.

All you “gurus” had to type was this:

speed = agent.velocity.magnitude;
 
            if (speed == 0) {
                GameObject.Find ("WBhorse").GetComponent<Animator> ().SetBool ("IsMove", false);
            GameObject.Find ("WBhorse").GetComponent<Animator> ().SetBool ("Idle", true);
        } else {
                     GameObject.Find ("WBhorse").GetComponent<Animator> ().SetBool ("Idle", false);
            GameObject.Find ("WBhorse").GetComponent<Animator> ().SetBool ("IsMove", true);

Instead I received semantic posturing, and running me around the mill, on a wild goose chase, for your own haughty pretensions.

Won’t be asking here again. Methos you are false. You and Lane are all phony. No answer given here was on base. It was all code skirting around the question. The example given by the “original helper” was simply a boolean “if moving”. This if moving boolean given above, is useless without the how if moving.

The code to check if moving, which was what I asked for was never given so that I could apply it directly, without misunderstanding, so to keep me on a string, and you all get on your high patronising voices so eagerly you cannot even see it, and instead you miss the context of what I ask, in your eagerness to be all guru, yet not show anyone the simple code needed. What a farce. You all band together like hawks looking for mice to peck on, here on the forum, I wonder if you actually get anything done. It is always the same on communications forums whereever you go.

If you want to help someone, help them straight out, if they want a hammer, give them a hammer. But you people give people snakes, instead of the fish they ask for. And you cannot even see it in yourselves, for if you did, you wouldn’t be that way. But here I am, finally after digging and digging, getting the code I need, which was what I directly asked for, to check for movement, and all of you posturing yet not provided, and saying I needed to go do long tutorials, taking time to write me long posts, about nothing, but to give me what I asked. False people. Shame. My code is now working, no help to you posturers, who spend more time saying what I need to do, before I can code it, then give me the code as I have posted above, which any of you all could have done in ten times less than the time.

All anyone had to do, was type the above. Because that is all I typed in, to get it working, instead I was given Links to sites, I had already been, long winded bag pipes out the mouth, about how I should learn, and never the code I needed to use, and learn from.

And now I see the code, and watch it working, this is learning, I have learned. But what I learnt from asking you people, is to not ask again. And to avoid you all, so you can slither around here together without me. Ha! Done! God is watching you all. None of you truly seek to help people, but seek to be patronising, like some father figure. It is pathetic. If you truly sought to help me, you would have typed what I needed from the start, and been done with it, which I made obvious what I needed.

Shake the dust off my feet, no more questions asked here about programming. And you all can band together pretending you were righteous, and that the “help” was already given, after running me on a wild goose chase. And that is what you all do, live in denial, of yourselves, staring at your computer all day. Ha!

I knew what I needed from the start. The code to get it working, Then now I know from that code. But you all were like, no no, that is to easy, you do not have “due diligence”, you have not gone through our tests, being judgemental. That is because you are too keen to make judgements rather than give help. And as I said to Lane in my video, on youtube, I see through it clearly. As I see many men this way. And it is much more apparent in this programming world, than my own world, where men like me, don’t behave like that, and are far more up front.

And now I come back here, and put my guru hat on, which is about how to be up front and give someone exactly what they seek, “give to those who ask”, and not give them snakes when they ask for a fish. Because I am wise enough in this world, that I do not need to be patronised that instead of receiving the code I asked, to go on tutorials. If you did not have the fish I asked for, meaning the code I asked for, then don’t even open your mouth to me, you vipers, for shame to do so. And ask yourself, why did you not simply type out what I myself have posted, short lines of code which is all I damn well asked for in the first place? Which was "how do I animate from an If move position, so to determine when I am moving, of which the animation was not the concern, but the how do I tell if I am moving?

I have not checked this thread since my last post, and this post is following from that, but I happened to see the Methos post above, typing this out, so addressed that one, but if there are any others I am not even going to read them, but wanted to show what should have been given from the start, before Lane came on and started posturing himself speaking for a communal group “us” to excuse not posting the code I asked for in fear that I had no due diligence. Well after reading the Methos post, more of the same old patronising, and no code, it appears he is speaking for a predator like “us” group that all have the same pretence to help people, who posture themselves rather than provide, that if they could not provide the objective code, they should put cotton wool in their mouths.

And if their are any forthright people here, not belonging to that “us” group who posture rather than provide, then I have not met them, because if anyone was deserving of being called a helper, he would have posted the code I needed, I would have tested it, it would have worked in a jiffy, I would have learnt by perusing that code, and saved myself 24 hours and strained effort. Yet because this logically, indeed did not happen, I can conclusively see that people help in pretence, and want to Lord it over people, the true help they ask. Now anyone can learn programming syntax, even the most nasty of people. But not many people can learn or have “due diligence” to always give to those who ask in what they ask, giving them the benefit of the doubt they know what they need, nor would they seek it. Now the true guru of gurus has spoken, and this thread is done.

Sorry you felt so slighted. Post #6 mentioned velocity and magnitude. There was another post here, I swear - by @GroZZleR about remaining distance (I think, and something else), but now I don’t see it.
Anyhow, good luck with your game :slight_smile:

Post 6, that had reference to the code only ‘about’ in abstract of the velocity, I had already been to that website about it. I already knew about velocity. I did not know how to apply it. I was asking for the syntax, and code example I could put with the code I had. When you read the site, that is the links leftyrighty posted, it only shows in prose to me about them, so there was navmeshagent.velocity and such. But I could find no clear example. If it is there, I could not find it. I had already made the effort to find it, and surely it was not much effort to type to me the line of code I needed. But I have gauged that people had ulterior and debased motives.

All someone needed to tell me was if (agent.velocity.magnitude);
One line, and I would have used it, and seen it worked, and been on my way. Then take a look at all the posturing that comes from Mr Lane Fox, and then he says, you will be hard pressed to get a pure example. One line is too much, but he writes me two paragraphs of posturing? Bah! The shame of it.

It was clear I already knew about velocity from my post, as was asking about it. I asked How do I do a IF Moving speed check from a set destination? Now that is explicit.

The answers I got. 1/ Check if moving boolean
2/ Use velocity magnitude. (yes but I needed an example, as further shown by my feeble attempts to put it all together in my following posts)
3/ Do tutorials (of which I already said I had been googling heaps and my ears bleeding from voices I do not wish to hear at all)

I had already sifted through the Unity documentation and could not pinpoint it. Someone surely had the time to type one sentence of code I needed, and instead I felt they were giving me everything but the answer. I have made my point clear, how the inclination to come and patronise me, rather than write that sentence of code directly. Then you came along methos5k and said the help was given. But it was not given, just as a person who cannot get up off the ground, help is not given, until he is lifted up, and you see him walking again.

I wanted the code, and I was not given it, and this in turn wasted my time for 24 hours, of which I had already spent 48 hours scouring the net for the solution, and caught in a search trap of legacy syntax from older versions of unity, blurring the issue for the current update methods, and so forth. And this whole shindig I found ridiculous, that a person requested I post a page of all my code, and all I got back was everything but the line of code I needed. Leftyrighty was on the right track, but I was looking for the code, as I had already considered I needed something to do with velocity, but did not know the perfect syntax for it, not just a link to a discussion about the code, not a boolean declaration either, which is only subjectively related, and certainly not patronising recommendations to do tutorials, and scour around the internet, when I had already done it, and come up blank. I am in the right here, and don’t I know it. If people do not wish to help in a direct an easy manner, as I have come here searching for that answer, then don’t even begin to post one, and if you are a person like that, any of you out there, don’t talk to me on here at all. Thankyou.