- Home /
Make a player jump when he jumps on a spring!!
Hi there!
I have a prop gameobject "Spring" with a box collider set to triger.
So, whenever player jumps over the spring, he gets lifted in the direction the spring is facing.
You can have the images for the mechanics of the props.
The above image shows the first mechanic. Player jumps over the spring, and he gets thrown upward.
Note: This upward direction is what I want. That is, the spring can be rotated in like left, right, up, and down direction.
I want the spring to throw the player in the direction it is facing the arrow. And, yeah, it includes diagonal push or throw. The picture below shows just that!
Now, I am not saying I can't do this simple problem.
I do have a solution to the problem i.e. The player already has character controller attached to it. Now, When player jumps on the trigger, the spring will add jumpSpeed to the playerController Script that controls the Charactercontroller.
Easily said, right?? That solution is for upward direction. But I also want downward, left, right, diagonal, etc. And, that through the character controller is hell!!
Complexity will rise when I tell that the Enemy AI can also use this spring. Now complexity goes sky high.
I was thinking the solution to be a better one. All I want is that the Spring applies a force or velocity on the Enemy or the player in the direction I specify it through vector3 mostly.
So, force should be able to do that, right??
Thing is, I do not know that!!!
So, any suggestions guyz!
Note: Game is being developed under Unity 3.5.7f6 Windows for Android.
Thank you for your support!!
[1]: /storage/temp/31004-case+1+-+state+3.png
[2]: /storage/temp/31003-case+2+-+state+1.png
Answer by Alessio89 · Aug 16, 2014 at 11:27 AM
You have many choices. I'll explain the two more obvious.
1) Use the UP vector of the spring When the player enters the trigger and press the jump key, just get the LOCAL up vector for the spring like this:
Vector3 springUp = SpringObject.transform.up
That way you can rotate the spring and still get the correct Up vector of the spring, then apply a force to the player equals to that up vector multiplied by the magnitude of the force.
2) Expose a Vector3 property in the inspector of the Spring You could also use a public Vector3 field and set it in the inspector. You then apply that vector as force when the player is in the trigger. This way you can tweak the direction and intensity of the force for each individual spring (some may be more bouncy than other?)
Hope that's what you wanted. If not, just be more specific and I'll try to answer :)
Oh, to apply a force you need a rigidbody attached to the entity and then through script use rigidbody.AddForce or rigidbody.AddExplosionForce.
Thanks for the answer, just what I wanted to know about!
I'll let you know how it went later !!
Did you figure it out? If you need any more help, just ask :)
Yes, I did figure it out. I first was using CharacterController, so rigidbody had conflicts with this approach.
So, I prototyped only with rigidBody and it worked well.
But, I was to use CharacterController strictly, so I had to rollback all that, and make custom script looking at your suggestions to create my own AddForcer() simulation using vectors.
Also, do check in on this issue! $$anonymous$$aybe you can help me on this!!
I'm using Unity 3.5.7f6 x86 on Windows 8.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to share the post to internet from game in unity3d? 0 Answers
calling java method from unity c# 1 Answer
About ListItems in Unity3D 1 Answer
How to Display high score using GUIText 0 Answers