Thank you for this simple and working solution! I wanna add that you can put movementSpeed in place of the 1.0f inside the clamp. Same result, simpler code.
@NECjAR6 жыл бұрын
You are very good in code and smart, man.
@adhochero66194 жыл бұрын
thanks. i was using .normalized before, but that would make it always 1 which means you couldnt move forward slowly like if joystick was only pushed halfway forward. but the clamp works great
@MoonLiteNite4 жыл бұрын
diagonal walking as was a "trick" in so many games of the 90s as a way to move faster,.. still see it working in some games to this day :D
@raptorswire72125 жыл бұрын
I believe you can also normalize your vector with something like: direction = direction.normalized;
@user-og6hl6lv7p5 жыл бұрын
direction.Normalize();
@carlabalos38843 жыл бұрын
But what happens is your controller moves a bit after releasing the movement button.
@avenemfilmes71012 жыл бұрын
using .normalized or .Normalize() will invalidate the acceleration of your character (since its gonna be 1, 0 or -1)
@avenemfilmes71012 жыл бұрын
@@carlabalos3884 if you are using the GetAxis, it will do, but if you are using normalize add GetAxisRaw
@MrBird945 жыл бұрын
Fantastic FREE lessons on Player/Character movement and allready so much better the the Uniity standardAssterFPSController.... haha !
@scottalexander_5 жыл бұрын
Cyborg PhotoshopVideos agreed!
@St4rdog5 жыл бұрын
For 'Move' you also have to multiply by Time.deltaTime after movementSpeed.
@muszen71644 жыл бұрын
i totally get what your talking about
@CameuhGames4 жыл бұрын
Thanks, that helped !
@WeegeepieYT6 жыл бұрын
Yeaaaay ^.^
@AcaciaDeveloper6 жыл бұрын
Meow
@snake_gaming01674 жыл бұрын
excelente tutorial, me has salvado de la frustración, saludos desde paraguay bro, ya me he suscrito :).
@ranama4 жыл бұрын
Thank you for this, it helped me alot!
@kevinbrabants1544 жыл бұрын
Thanks so much lol, This is super useful there is a little more you could explain to use it in a more complex way because some people might not find out although its not really ur job so oh well lol.
@deadlysteel3005 жыл бұрын
Love this
@vincentj20246 жыл бұрын
Love the tutorials so far! I do have one question though. I have noticed while jumping you can only jump in a specific direction. How would you go about moving while in the air(during the jump)? Thanks
@AcaciaDeveloper6 жыл бұрын
Very good question! This is how I'd probably solve this problem: While the character controller is in the air, the SimpleMove function will cease to apply any new force until the character controller is grounded again, so when the player leaves the ground, you'll want to instruct your program to switch from using SimpleMove to Move since Move is able to operate in mid-air. As you can imagine, we pass the same Vector3 value into the Move function as we did the SimpleMove function, but we append an extra multiplication by Time.deltaTime since (unlike the SimpleMove function) the Move function is framerate dependant: charController.Move(Vector3.ClampMagnitude(forwardMovement + rightMovement, 1.0f) * movementSpeed * Time.deltaTime); Ideally, within context of the code in this video series, the value passed into this function could, and probably should be combined with the function call that we make to Move within the JumpEvent() coroutine to push the player into the air (It's recommended that you only make one call to Move and SimpleMove per-frame). One thing you'll want to execute ONCE at the beginning of every jump is this: SimpleMove(Vector3.zero); This makes sure to zero out any forces applied by SimpleMove prior to the jump. This is what we want, since the Move function will take over from SimpleMove as soon as the player leaves the ground. The example code snippets above are only there to help you understand, and the implementation is probably a bit more complex, but I hope this helps point you in the right direction.
@vincentj20246 жыл бұрын
That was exactly what I was looking for, thanks!
@TheXenoXD6 жыл бұрын
@@vincentj2024 Would you mind letting me know what exactly you did to make this work? Been trying for a bit now and it might just be getting late but i cant figure it out :/
@TheXenoXD6 жыл бұрын
@@AcaciaDeveloper Or perhaps you yourself could give me a little more insightr into this?
@goncharov0006 жыл бұрын
@@AcaciaDeveloper yes please elaborate on this!
@forcepower71166 жыл бұрын
Please do a Tutorial on Crouching
@forcepower71164 жыл бұрын
Panda Brasil ?
@harleybekker6 жыл бұрын
thanks man :)
@n9it4 жыл бұрын
i wanted to know how to fix this diagonal issue with the brackeys code
@pedrohenriquedrawanz88033 жыл бұрын
you still need help? i know this was 7 months ago but if u didn't give up yet i could help u
@Rosmarodin2 жыл бұрын
@@pedrohenriquedrawanz8803 I'm also having trouble fixing the diagonal issue with Brackeys' code
@pedrohenriquedrawanz88032 жыл бұрын
@@Rosmarodin Bro, that was a whole year ago, i don't remember how, i'm sorry, but if i'm not wrong u can just review the code from other similar videos and one of them will answer that question
@RetroAnt34 жыл бұрын
Great help. Thank you
@pixelscholar6 жыл бұрын
Worked like a charm. At its heart, we're essentially tweaking a matrix transform with this little addition to the code right?
@AcaciaDeveloper6 жыл бұрын
This portion of the code does not tweak anything like that directly. I'm simply clamping the x, y and z elements of a standard Vector3 object to 1, and it has nothing to do with any matrix transformations at this point. It's not until the vector is passed through into the SimpleMove function that its value has any effect on the game object's position, and thus, the values stored within its underlying matrix.
@null9876 жыл бұрын
First off all, this is a great solution for the problem previously I had just used an if statement but I'll change up my code to include this solution. Q: I noticed that when going down a slope my character controller bounces instead of moving along the slope, I realize that the problem is that the character is moving faster in the x,z plane than the velocity at which he is falling, so far I've got a solution but it only works on stairs do you by any chance know how I can solve this problem in a more elegant way than just applying a downward force when the player is on stairs since that method feels like brute forcing my way through the problem instead of solving it and it doesn't even work on general terrain but only on pre-made stairs and ramps? PS: Love your work, and sorry for the long question!
@AcaciaDeveloper6 жыл бұрын
Hello, thanks for the complements and I don't mind long questions. This is in fact a topic planned for the next video in this series. My system works by shooting a ray from the player's position downward. If the surface's normal below (that the ray hits) is not equal to Vector3.up or (0,1,0), the surface is sloped and thus (similarly to your solution) applies an additional downward force. I've tested this on a terrain with the jumping mechanic, and it seems to work well without any issues (to my knowledge), so my solution may be more beneficial to you. I have looked for more elegant solutions in the Unity forums and other sites, but most seem to point toward this method of adding more force. The only other way (I can think of) is not to use the physics system at all. E.g you could shoot a ray directly downward from the player, get the position of the hit point on the floor, and set the player's position directly to a point that is n number of units higher on the Y axis than this point on the floor (where n is probably half the height of the character controller). This method has been known to be a bit jittery though which would probably prompt additional function calls for lerping etc. Personally, I'd choose the method of applying extra force within the physics system, since it seems to fix everything in one simple function call. Hope this helps anyway.
@null9876 жыл бұрын
Thank you very much for the answer, I have also seen raycast being used in unity forums and I believe that is the best solution available right now! I'll do some research on it and I'll make sure to watch your video on the subject as soon as it comes out!
@stephencooper87105 жыл бұрын
Great tutorial I want to be able to move up and down but not a jump effect so that if I move up I stay at that position but I can only move down to the ground position at the lowest point is this possible?
@Mark-vt7pl4 жыл бұрын
Great tutorial. How would you implement an inverted y axis for the camera?
@Mark-vt7pl4 жыл бұрын
Nvm i figured it out, you just change the transform.Rotate(Vector3.left * mouseY); to transform.Rotate(Vector3.right * mouseY); :D
@weegismrts4 жыл бұрын
camera animations? like jumping and walking
@urusuru37105 жыл бұрын
Sweet
@devstandgames4 жыл бұрын
Thanks you saved my ass once again
@johannb.rastarson35235 жыл бұрын
This is the only video I've found on this topic but this makes jumping impossible for my Movement Controller script :(
@Semaj0z4 жыл бұрын
Can u paste ur script here?
@johannb.rastarson35234 жыл бұрын
@@Semaj0z Lol, I have fixed it
@Mikelica693 жыл бұрын
@@johannb.rastarson3523 how?????
@petermoss73874 жыл бұрын
i once heard vector3.clampmagnitude is better than .normalized because it's easier to process. thanks again
@yasin87096 жыл бұрын
Thanks :)
@RM.-_-.3 жыл бұрын
Still moves twice as fast diagonally
@bity-bite4 жыл бұрын
In Godot, it is just "Velocity = Velocity.normalized()"
@LoganMTM4 жыл бұрын
You just remove the most precious Doom bug
@Mars-zr5op5 жыл бұрын
im making a 2d top down rpg, but i cant prevent diagonal movement, here is the script im using, using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class P1C : MonoBehaviour { public float speed = 0.75f; private Rigidbody2D myrigidbody; private Vector3 movement; private Animator animator; private BoxCollider2D BoxCollider; void Start() { animator = GetComponent(); myrigidbody = GetComponent(); BoxCollider = GetComponent(); } private void Update() { movement = Vector3.zero; movement.x = Input.GetAxisRaw("Horizontal"); movement.y = Input.GetAxisRaw("Vertical"); UpdateAnimationAndMove(); } private void UpdateAnimationAndMove() { if (movement != Vector3.zero) { MoveCharacter(); animator.SetFloat("moveX", movement.x); animator.SetFloat("moveY", movement.y); animator.SetBool("Moving", true); } else { animator.SetBool("Moving", false); } } void MoveCharacter() { myrigidbody.MovePosition( transform.position + (movement * speed * Time.deltaTime)); } }