Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

Resolved I cant move rigidbody

Discussion in 'Visual Scripting' started by Sanctu, Feb 17, 2021.

  1. Sanctu

    Sanctu

    Joined:
    Sep 28, 2020
    Posts:
    20
    Hello!

    I have a problem when I try to synchronize a certain movement with a blender animation, for example, to jump, I can't do it.

    And trying simpler things, without synchronizing the movement with the animation, everything I do, trying to use addforce or addrelativeforce, for example, pressing space to jump, without more, does not cause any kind of displacement in my rigidbody.

    I don't know where the problem is, or if I'm doing it wrong. In principle, the code is that simple, to make it jump, right?
     

    Attached Files:

    • 1.jpg
      1.jpg
      File size:
      29.2 KB
      Views:
      46
  2. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    134
    ericb_unity likes this.
  3. Haneferd

    Haneferd

    Joined:
    Feb 3, 2018
    Posts:
    33
    There are probably a lot of ways of doing this. I have made two short visual scripts below (In Unity 2021.1.0b6) to display it. I like to use velocity change instead of add force. Also added a quick and dirty check if player is grounded before jumping, and also to make sure that the animation is idle when grounded, and jumping if jump. The animation is updating the boolean variable on the transition in animation. I just made a boolean called "Jump":
    upload_2021-2-17_13-32-32.png

    And for the Animation to Work:
    upload_2021-2-17_13-33-13.png

    And the animation controller to illustrate:
    upload_2021-2-17_13-35-24.png

    Hope this can help :)

    Happy gamedev!
     
    Sanctu and ericb_unity like this.
  4. Sanctu

    Sanctu

    Joined:
    Sep 28, 2020
    Posts:
    20
    I have created both scripts, and even that I checked that the kinematic option is not checked, and it still does not work, there is no way for the Rigidbody to move, I do not know where the problem is.
     
  5. Haneferd

    Haneferd

    Joined:
    Feb 3, 2018
    Posts:
    33
    Something to check:

    Did you change from impulse to velocity mode in the add force ?

    The mass in the rigidbody, is that a large number? or only one?

    Have you tried to put in a large number in Y-axis (Add Force) ?

    Are your coordinates X and Z in the plane, and Y upwards ? (Sometimes, Z-is used upwards.)

    Have you added a "Character controller" ? That does not work any good with Rigidbody.
     
    Last edited: Feb 17, 2021
    Sanctu likes this.
  6. Haneferd

    Haneferd

    Joined:
    Feb 3, 2018
    Posts:
    33
    Lets try a very simple jump then, with simple graphics.
    For the visual script over, I changed the movement to Rigidbody.Set Velocity. And the recast distance to 1.

    The Ground is a cube 10x0.5x10 as the screen below (with the jump script): upload_2021-2-17_16-42-19.png

    The Cube in the screen has these properties:
    upload_2021-2-17_16-44-15.png

    This should work for you ??

    If using Bolt (And not the newest Unity Visual), you need to replace "If" with "Branch".
     
    Sanctu likes this.
  7. Sanctu

    Sanctu

    Joined:
    Sep 28, 2020
    Posts:
    20
    Thank you very much for the inconvenience man!

    But it still doesn't work, let me explain:

    With the cube, it works perfectly, I follow your steps and there is no problem.

    But I have tried it with another pj, different from the first one, and I have managed to make it jump, but deactivating the animator and nav mesh agent, with these two active, it doesn't work either, so it doesn't work either.

    Finally I have tried again with the first pj, and it does not work in any way. Yes, this pj has animator too, and character controller, but if I deactivate both, the pj sneaks into the field, and the code doesn't work either, the "branch" always throws me negative.
    (both pj, have the same mass as the cube)

    In addition to this, now I have another question, if this gives problems with the character controller, which obviously I need to control the pj, how can I do all this, whether it jumps or moves forward in an attack animation for example, without the character controller? or how is it compatible with it?
     
  8. Haneferd

    Haneferd

    Joined:
    Feb 3, 2018
    Posts:
    33
    Okay. Last attempt today :)

    I have updated the box with Character controller, and removed Rigidbody (Rather make gravity myself).

    The player setup is like this:
    upload_2021-2-17_23-57-30.png
    As you can see. No Rigidbody, only player controller.

    Then, the scripts...

    One movement script and one jump script. I have made some comments in the script grouping. The scripts are in the same graph:

    Graph Variables. I have added three variables. One for walking speed, one for jumping speed, and one for gravity:
    upload_2021-2-18_0-1-10.png

    Walk script: I get axis and create vector3 movement. Multiply with movement speed for Z- and X- direction. For the Z- direction, I have added gravity. Finally I multiply the final vector3 with Time.DeltaTime so it comply with frame rate.
    upload_2021-2-18_0-4-3.png

    For the jumping, I use the character controller and check if it is grounded. If so, and the spacebar is pressed, I get the current velocity (If player is moving when try to jump) and extract the Z- and X- axis out and add jumpSpeed to the Y- axis. Then create a Vector3 and multiply it with delta.Time before putting into Character move:
    upload_2021-2-18_0-7-13.png


    If this work, then you can add motion into the animation controller for movement. The animation controller is a state machine, so it can handle most of what you need :)
     
    Sanctu likes this.
  9. Haneferd

    Haneferd

    Joined:
    Feb 3, 2018
    Posts:
    33
    Just had to create a terrain with the cube and the logic :)

    Made a short movie of the result:
     
    Sanctu likes this.
  10. Sanctu

    Sanctu

    Joined:
    Sep 28, 2020
    Posts:
    20
    Oh man! what a job! Thank you!

    Yeah, this finally produces movement in my pj, I have eliminated the rigidboy, but, it does not work well at all, I explain:

    Yes, the pj jumps like the video cube with these scripts that you have taught me. But first, to jump a little bit, I need to set the jumpspeed to almost 80, and it jumps right up. When I hit the space, it jumps suddenly, Fall, it falls well, it falls slowly like the cube, but it jumps super fast, more than a jump, it seems an instantaneous change of position ...:(
     
  11. Sanctu

    Sanctu

    Joined:
    Sep 28, 2020
    Posts:
    20
    I've been testing things, and I've gotten the pj to jump, like the cube.

    But I had to disable the animator's apply root motion (this is correct? Right?) This way if it works correctly as the cube at last. Although the grounded branch always gives me negative, and I have had to remove it.

    I have tried to implement this with my pj's movement system (which rotates and moves with the corresponding animations) but I can't make it jump as with your system, no matter how much I try to copy and adapt it to mine. I show you my movement system, to see what you think.

    Another question I have is that with this system (the system of the cube) it only works if I jump, but, if I wanted that when pressing the space, it was propelled forward? Because if I change the vector "y" of the jump, for the "x" or the "z", it does not move the same as in the jump.

    Sorry if my doubts are a little obvious, I am new to using bolt and programming
     
  12. Haneferd

    Haneferd

    Joined:
    Feb 3, 2018
    Posts:
    33
    I am currently not at PC tonight, but two things;

    1. If you try to use the check if grounded logic with the raycast option, it uses a max distance. And if you have a large character and for some reason the raycast starts from center of the character, the ray will never hit any collider at the ground if distance is not large enough. So you have to check that.

    2. Your logic looks like it will try to play two the animation in both direction at the same time. It also have some strange connections at the end. I can try to ahow you tomorrow if you dont figure it out. Try to get it working in one direction first.
     
  13. Sanctu

    Sanctu

    Joined:
    Sep 28, 2020
    Posts:
    20
    Yes, my fault, I have checked it this afternoon, and I have been able to fix it, there is no problem with that.

    Yes, there are two animations, it is actually the same, the running animation, but this way I get the animation to work from left to right, from top to bottom, and even diagonally. If I don't do it like that, even if the character moves in all directions, the animation only works if it goes from left to right, or from right or left, that is, horizontally, in all vertical movements, the animation does not work. It works at the same time with another turning system, so that the character can turn on himself.

    I'm trying to spin my system with what I see in yours, even in one direction, it doesn't work ...:(:confused:
     
    Haneferd likes this.
  14. Haneferd

    Haneferd

    Joined:
    Feb 3, 2018
    Posts:
    33
    I am not very skillful when it comes to handle animation, but is your character/player a Rig? Meaning that it is a collection of arms/legs/head?

    I can download an asset and try out some movement with a character rig.

    I ususally use boolean values for move, jump, attack, idle.

    Is it sword/axe or guns your character carries?

    Are you moving in an open map? In all directions?

    Are the camera following from behind or is it static in one direction?
     
  15. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    26,915
    I am not sure what this means but I certainly find it remarkable.
     
  16. Sanctu

    Sanctu

    Joined:
    Sep 28, 2020
    Posts:
    20
    I meant, rigidbody, sorry. because @Haneferd, he told me that for these movements that I want to do, that it is better to do it with the character controller than with the rigidbody,
     
  17. Sanctu

    Sanctu

    Joined:
    Sep 28, 2020
    Posts:
    20

    Yes, exactly, it is a rig, with arms, legs ...

    I used a mixamo pj, (xbot) and one of their running animations to test all of this.

    No, he has no weapons or sword.

    Yes, it is an open map, and it moves in all directions with wasd. And I have put the camera, as in Diablo III.
     
  18. Haneferd

    Haneferd

    Joined:
    Feb 3, 2018
    Posts:
    33
    I have downloaded a free character rig from asset store, but I must admit that it is quite difficult to get it all playing properly. So, I need to investigate a bit further. Musch easier with only a cube :)
     
  19. Sanctu

    Sanctu

    Joined:
    Sep 28, 2020
    Posts:
    20
    Yes!! There is the problem, that making everything work with the animations and the movement together, is where I get lost XD I wish it were as easy as with the cube. XD
     
  20. Haneferd

    Haneferd

    Joined:
    Feb 3, 2018
    Posts:
    33
    Answer-Part-1

    I think I got the Movement thing going here. But, when investigating the Animation-rigs, it seems that you can decide to use the animation for jumping. The Animator - Update setting, can be set to "Update Mode: Animate Physics" . Not sure what that will do, but that is probably for some other forum-post.

    Anyways. Here is what I achieved. Jumping and moving.

    I downloaded a free Asset:
    https://assetstore.unity.com/packages/3d/characters/low-poly-toon-adventurer-male-78374

    I used the Prefab and changed almost all settings in CharacterController (Which I added), so it touched the ground. I removed the C# script, so I could make it in the Visual Script (Added a Graph). Also kept the Animator - Demo Controller. The result:
    upload_2021-2-20_21-28-9.png

    Then I created a Terrain, and added some trees and cubes.

    The Video shows the result:



    So, for the Visual Scripting Logic.

    I created a sequence of steps in the Update (I'll detail it below)
    upload_2021-2-20_21-39-35.png
    Also created some graph variables to steer the walking speed, jumpHeight, gravity and playerVelocity. Also made a boolean isGrounded, as this is a problem to get right:
    upload_2021-2-20_21-38-17.png


    For the update

    sequence-0 (It is the ground check)

    It gets the position of the player, then recast downward (Direction 0,-1,0 = Downwards in the Y-axis). The Max Distance must be larger than half of the player (It depends where the origin point of the player is.. Maybe check and try out)
    Remember to change the Layer Mask on the terrain to Ground!

    upload_2021-2-20_21-41-37.png
     
    Sanctu likes this.
  21. Haneferd

    Haneferd

    Joined:
    Feb 3, 2018
    Posts:
    33
    Answer-Part-2

    Sequence 1 - Update the velocity if player velocity.y is less than zero and player is on the ground

    Check if grounded. If yes, check player velocity. and if it's negative, update the velocity variable with X,Z and Zero for Y.

    upload_2021-2-20_21-48-27.png


    Sequence 2 - Get directional inputs and do a simple move, and play animation.

    GetAxis for X and Y and create a Vector3. Check if there are movement, if so, multiply with speed and set direction of player in the movement direction. Do then a SimpleMove (Not Move), and start animation
    upload_2021-2-20_21-51-25.png


    Sequence 3 - Create some Gravity for the player, so if jumping it will be smooth to land. Do a CharacterController.Move

    Get the saved player velocity and create a vector3. Then update the graph variable velocity and do a CharacterController.Move. This is the Final Sequence for the Update per Frame.
    upload_2021-2-20_21-56-19.png


    So, the Jumping logic:

    It updates the player velocity variable. It also smoothes the jump, so it is not flying directly with a square-math function:
    upload_2021-2-20_22-0-32.png
     
    Sanctu likes this.
  22. Haneferd

    Haneferd

    Joined:
    Feb 3, 2018
    Posts:
    33
    Answer-Part-3

    And so, just the remains attack and defense of the player. I guess it is self-explained:
    upload_2021-2-20_22-2-18.png

    For the Player follow. I created an Object variable on Camera, and a flow script there
    upload_2021-2-20_22-7-47.png


    So, Finally.. :)

    Hope you can use some of it. I have learned a lot!
     
    Sanctu likes this.
  23. Sanctu

    Sanctu

    Joined:
    Sep 28, 2020
    Posts:
    20
    oh man! You have passed! Thanks a lot!

    I think it will help me, this weekend I will not be in front of the pc, but on Monday I will tell you the result! But looking at it, it's great! More than I expected! Thanks a lot friend!:);)
     
    Haneferd likes this.
  24. Sanctu

    Sanctu

    Joined:
    Sep 28, 2020
    Posts:
    20
    It works perfect, man!
    A perfect help! Very thankful!

    Thanks a lot! And thank you very much for the inconvenience!
     
    Haneferd likes this.
  25. Haneferd

    Haneferd

    Joined:
    Feb 3, 2018
    Posts:
    33
    No worries, glad to help. I am trying to learn all the caveats of Unity myself, and answering questions by testing out things makes me a better Unity developer too
     
    Sanctu likes this.
  26. Sanctu

    Sanctu

    Joined:
    Sep 28, 2020
    Posts:
    20
    Let me ask you one more question, with this visual scrpit, I can get that instead of jumping up, in static, jump forward in the direction the character is looking?

    Because I get him to jump forward, or even backward, but I can't get him to jump in the direction he's looking, he always does it in the same direction :(
     
  27. Haneferd

    Haneferd

    Joined:
    Feb 3, 2018
    Posts:
    33
    I am not at Mac now, but in the jump script. If player is standing still, X and Z will be zero. You can add some force in forward direction. Something like vector3.forward after the vector3.create node, or add a value in the Z axis in the vector3.create node at the end of the jump script
     
  28. Sanctu

    Sanctu

    Joined:
    Sep 28, 2020
    Posts:
    20
    Yes, that's what I'm trying, vector3.fordward, I can't get it to do anything.

    And adding value in z, I get it to move in z, or if I do it in x, in x, while it jumps, it advances, yes, but the problem is that it always does it in the same direction. For example, if the character looks to the right, with those values, he jumps forward without problem, everything is correct. But if the character looks to the left, he keeps jumping to the right ... And if I change the values so that he jumps to the left, he doesn't jump to the right ... I mean, he only jumps in the direction that I enter the values, Because I don't know how to make this move in the direction he's looking.
     
  29. Haneferd

    Haneferd

    Joined:
    Feb 3, 2018
    Posts:
    33
    So, do you mean to change direction of the jump while in mid-air?
     
  30. Sanctu

    Sanctu

    Joined:
    Sep 28, 2020
    Posts:
    20
    No no, I mean I want him to jump forward, in the direction he's looking. For example, the character in static, looks to the left side, if I press space, let him jump forward in that direction (left), if the character, look to the right, let him jump to that side (right).

    What I mean is that he jumps and moves in the direction he is looking, without having to run. The character, being in static, will be able to propel the jump in the direction in which he is looking.

    I don't know if I'm explaining correctly, or I'm confusing you xD
     
    Haneferd likes this.
  31. Sanctu

    Sanctu

    Joined:
    Sep 28, 2020
    Posts:
    20
    @Haneferd Do you know any solution to do this? I, however much I try with this virtual one, the only thing I get is to make it worse every time XD
     
  32. Haneferd

    Haneferd

    Joined:
    Feb 3, 2018
    Posts:
    33
    @Sanctu I am not sure, but You might need to catch the direction and store that in a variable to be used as a direction parameter for directional force in the jump
     
  33. Sanctu

    Sanctu

    Joined:
    Sep 28, 2020
    Posts:
    20
    And when the direction is captured, how do I add to that parameter a value that always moves it towards the direction you are looking?
     
  34. Haneferd

    Haneferd

    Joined:
    Feb 3, 2018
    Posts:
    33
    The jump parameter is Y axis, so I guess you have to add speed to the transfrom forward. If you use Transform forward get - then it should return a vector3 , and then you multiply that with speed and time.delta time.
     
    Sanctu likes this.
  35. Sanctu

    Sanctu

    Joined:
    Sep 28, 2020
    Posts:
    20
    Ok, i will try.
     
    Haneferd likes this.
  36. Haneferd

    Haneferd

    Joined:
    Feb 3, 2018
    Posts:
    33
    When you try, add a debug-log of the vector3 outputs and see the values captured in the console. Then you’ll see the different values of the differen positions
     
    Sanctu likes this.
  37. Sanctu

    Sanctu

    Joined:
    Sep 28, 2020
    Posts:
    20
    I got it! As you said! I don't know if I have reproduced it correctly, but I have made it move forward, too. Thanks a lot!!
     

    Attached Files:

    • 1.jpg
      1.jpg
      File size:
      58.4 KB
      Views:
      23
    Haneferd likes this.
  38. Haneferd

    Haneferd

    Joined:
    Feb 3, 2018
    Posts:
    33
    @Sanctu Fantastic, good work!. I guess the thread can be closed? :)
     
    Sanctu likes this.
  39. Sanctu

    Sanctu

    Joined:
    Sep 28, 2020
    Posts:
    20
    Yeah! It can be closed! Ty!! ;)
     
    Haneferd likes this.
unityunity