- Home /
2.5D Platformer - Jump Question
Noob question for a 2.5d platformer -
Should I use transform.translate for left and right movement, and a physics 'addforce' for upward thrust of jump ?
Currently I calculate a 'moveDirection' using Input.GetAxis("Horizontal") and I add a 'moveDirection.y = jumpHeight;' when jump key is pressed, and then finally apply it all with
controller.Move(moveDirection * Time.deltaTime);
I can upload my entire code if necessary but I think you'll get the idea. It just seems that there are so many ways to move things and I'm having trouble figuring out what's best.
P.S. is it possible to do a comparison on a translate position and a float like this:
if (gameObject.transform.position.y <= jumpMaxFloatVar)
it doesn't seem to work, I can print out gameObject.transform.position.y but my guess is it's not a float ?
Answer by Proclyon · Apr 13, 2011 at 11:06 AM
As I have said before in several answers, the translation concept is often confused for movement. This is a common misconception as movement is the continous flow between two points, the transform property can manipulate time and space and go to places by changing the x,y,z variables. If done over time it looks like movement , but actually the engine will not see this as movement and thus ignore events such as collisions.
You avoid this problem by using a character controller object attached to your "player" object and manipulate that with it's move function which will , as the name implies, move the character, rather then teleport it a lot in very little time.
Physics can be used to literally kick and push something around, but imagine in real life what that would be like? Jumping by applying a kick at the right moment? Sure, easy, and now navigate a maze? Would that be harder? What about slopy mountains with ice patches and wind blowing around? It's going to get really hard really fast making it a not so worthwhile time investment to apply physics to jump in my experience.
So the conclusion I will make for your suggested approach:
Should I use transform.translate for left and right movement, and a physics 'addforce' for upward thrust of jump ?
I would say, No, use character controller options instead and keep in mind that if you change gravity in the game or in the future of the game, you need to have considered that in the design. So jumping with +9.81 all the time could be a problem later if you want to build a low-grav mode such as in the Unreal Tournament Environment.
SideNote: All these answers are actually quite easy to extrapolate from the great naming conventions the unity3D group has given their objects and properties. They don't just call a PLUS a MINUS (which some pesky students may overload to spice up your monday). Character Controller really means, it controls the character. These names should be a big marker for everyone to consider using it for that goal , and avoid sidetracking untill ABSOLUTELY NECESSARY!
Using addforce wouldn't prohibit the use of any sort of gravity effect. It'd be very easy to set up a trigger (a new room) to send a new variable (or, modifier to multiply against the normal jump height) to act as a new jump speed. Then you could have low grav with fans at the top blowing you back down =)
I don't think "it wouldn't work well" is a good response for unity. "so and so may be easier to work with" or "this or that is more efficient" on the other hand are common problems.
Thanks for contributing an answer to Stack Overflow!
This is a Q&A site, not a discussion forum, so please make sure you answer the question.
Provide details and share your research. Avoid statements based solely on opinion; only make statements you can back up with an appropriate reference, or personal experiences. <-------- Personal experiences. It's not unity3D forum but it'll do
Thanks Proclyon ! Sorry about the delay in getting back to you but I did read this as soon as you wrote it and this was very helpful. Although I've been jumping from Unity to 3ds $$anonymous$$ax to Blender a lot lately in my College Course, I'm finally getting to grips with the simple scripting stuff in Unity. Attempting some raycasting today. I'll see how I go. And yes, the function/class na$$anonymous$$g conventions are ace, as well as the script reference, which is also amazing. Thanks for your time.