Having a problem with my model, keep moving on either one of the axis I but it on ( X/Y/Z )

Hello,

I’m trying to see why my model keeps moving whenever I finish putting this command here in the script, " moveDirection.y=jumpForce; " after I press the " Play " button, I didn’t know if this needed tec-support and I had someone helping me before but they are away for some time, so I thought I could try here and see if I could get help here.

To start this off I needed to follow a script here in one of the images and make my own out of it, and before having " moveDirection.y=jumpForce; " I was able to have the " Up Arrow " key as my models run animations and it would show the run animation once I press that key and stops once I let go of it as well. Though like I said once I put in " moveDirection.y=jumpForce; " at the last part there, I go and press " Play " then out of nowhere my model would go straight up on the Y axis or lets say I changed the " y " to either " x ", or " z " it would keep moving left/right or forwards/backwards. ( For " y " I only say it’s going up because I have a " Plane " under it because of the gravity I needed in the script ).

I have tried to see what might be causing this and I think it’s the " moveDirection.y=jumpForce; " like said because once I remove that part in the script, everything seems to go back to normal. Which I say back to normal, is because it wouldn’t do the run animation I have setup for the " Up Arrow " key though it would stay in the " Idle " animation I have setup.

I have tried remaking the script and adjust somethings but for some reason everytime I put in " moveDirection.y=jumpForce; " it won’t do any other animation except the " Idle " one and it would keep moving on either one of the axis if I change it to either, " y ", " x ", and " z ".



Here’s an approach you can use to help you learn more about what is actually happening:

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

You must find a way to get the information you need in order to reason about what the problem is.

If this is ok to ask, I looked at the link that you provided, and I was checking to see on how to do a " Debug.Log() " and saw that you said to put it right on the top, and I tried that and typed in " Debug.Log() " on the beginning of the thing I had but I don’t think anything happened. Unless if you’re talking about the " Debug " button that is in between " Build " and " Team ".

Though if those aren’t it, I do apologize if I’m getting this confusing and/or wrong, this is my first time doing these scripts and I’m trying to wrap my head around this mainly because I haven’t gotten too much experience with this with it.

Go check out the docs for this. Debug.Log() is how you write information at runtime to the console.

This tells you if the code is running, and can tell you more data.

For instance, try this in code:

Debug.Log( "jumpForce = " + jumpForce.ToString());

and this can help you figure out what data is going through your code.

Oh ok, so once I type in " Debug.Log() " inside of the " () " I could put in this code here " moveDirection.y=jumpForce; " that seems to be causing the problem and see whats going on, and I could check the console for what’s the problem with it. I think I’m starting to understand, also that code you gave me there called, " Debug.Log( "jumpForce = " + jumpForce.ToString()); ", I put that in and went back to Unity and press the " Play " button and looked at the console and kept seeing this come up, " jump.Force =10 UnityEngine.Debug:Log(Object) ", I believe that that’s nothing wrong I think or I could be wrong?

It is your program. What do you expect this value to be? Is 10 a reasonable value? If so, then move onto other values and try to track down what is going wrong. Nobody here can do this for you. You have it all on your machine.