- Home /
Interpolation with jumping - Acting Weird.
Here is my player controller I have been working on.
Everything works fine, finally found out that using Interpolation for jumping smoothed out the jitteryness of jumping, which thankfully I found.
But now I am running into a stump in the road.
I can still jump fine when I stand still, however, if I start moving, I jump maybe not even half way as high. I tried using Extrapolate or whatever, it was worse than not having Interpolate, but here is my code.
//private CapsuleCollider col; // This is the Collider for Player.
private Animator anim; // This is the Animator for Player.
public float Speed = 5.0f;
public float TurnSpeed = 5.0f;
public float jumpHeight = 10.0f;
public bool canJump;
public bool Transition = false;
// Use this for initialization
void Start () {
anim = gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
if (!Transition) {
if (Input.GetKey (KeyCode.W)) {
transform.Translate (Vector3.forward * Speed * Time.deltaTime);
anim.SetFloat ("Speed", 1f);
} else {
anim.SetFloat ("Speed", 0f);
}
if (Input.GetKey (KeyCode.S)) {
transform.Translate (Vector3.back * Speed * Time.deltaTime);
anim.SetFloat ("WalkBack", -1f);
} else {
anim.SetFloat ("WalkBack", 0f);
}
if (Input.GetKey (KeyCode.A)) {
transform.Rotate (0,-3,0 * Time.deltaTime);
}
if (Input.GetKey (KeyCode.D)) {
transform.Rotate (0,3,0 * Time.deltaTime);
}
}
}
void FixedUpdate(){
if (Input.GetKeyDown (KeyCode.Space) && canJump == true) {
if (canJump) {
anim.SetBool ("Jump", true);
transform.rigidbody.AddRelativeForce (Vector3.up * jumpHeight);
Physics.gravity = new Vector3 (0, -20, 0);
}
}
}
void OnCollisionEnter(Collision col){
if (col.gameObject.tag == "Map") {
anim.SetBool ("Jump", false);
}
}
}
Please, if you can help me, all would be greatly appreciated!
I'm not trying to bump this, but I really need some kind of help with this, please...
To be honest I'd rather not even use forces for jumping, I don't see why a transform.Translate (Vector3.Up 10 Time.deltaTime); won't work.
But now I have the jumping working with forces, but now, no matter what I do, my turning is all weird.
I put physics materials on ground, I drag the Drag and $$anonymous$$ass up and now I'm getting the most akward kind of turns.
But I don't get WHY I can't just use the Jumping force with my normal transform.Translate.. The result = I can jump fine when standing still, but when I move, the force is cut in half, but when i have forces doing my movement, the jumping force is fine, but then a transform.Rotate doesn't turn my player currectly as the Forward pushing force is trying to slide me, no matter what settings I have changed.
This is getting on my last nerve. Please help me guys!
Could be wrong but seems like you're doubling gravity in your jump function. logically you would need twice the jump force?
As for sliding, you need your damping on the rigid body roughly 10x your movement force. $$anonymous$$g. A linear movement force of 1 with a damping of 10 give you a sharp but smooth deceleration like the old mario games.
Yeah i was doing gravity like that as it kept my player to the right height that I wanted, if I took it off, he would just slowly fall to the ground, like really slow. Figured it would be better to do it that way than doing it in Unity, as I may not want anything else to fall that fast as it's supposed to be ("real") life gravity already set up isn't it?
I'll try the damping and stuff, damping is the Drag right?
Right now all I know is, when it turn, it's like a Dirt Bike turning, the bottom of the player appears to be like hitting a "Rut" because the forward direction of (force) still slightly pushes as I am rotating.
Yea drag is the right one. It should fix that for you. In my experience 10x the input force gives a rapid deceleration, but doesn't take so long it looks like you're on ice. If you want to stop quicker just ramp up the drag further.
As for gravity, turn it off on that rigid body and apply it yourself. In most games a gravity of -20 to -30 for jumping looks pleasing. This way you can have it only affect that object.
Answer by unimechanic · Oct 08, 2014 at 02:08 PM
There are three very common pitfalls when using physics in Unity 4.x and lower (that use Nvidia PhysX 2):
Altering static colliders.
Altering a GameObject that has a Rigidbody without using the Rigidbody members.
Altering a Rigidbody outside of FixedUpdate.
Altering means: enabling (i.e. SetActive) or transforming (moving, rotating, scaling). So, whenever there is a GameObject with a Rigidbody, it must always be transformed with the corresponding Rigidbody's members, and always inside FixedUpdate. I see you are using both Update & FixedUpdate, and Transform & Rigidbody operations.
Make sure of the following:
If the collider is loaded/unloaded along with the scene and shouldn't be altered in any way, it can be a static collider.
If the collider is created/destroyed dynamically or is modified in some way, it must be a dynamic collider.
More on static colliders here:
http://docs.unity3d.com/430/Documentation/Components/DynamicsGroupOverview.html
Doing otherwise will corrupt the physics scene and physics operations could fail, until it's reconstructed, which is expensive and will happen at some unknown point. This is an indirect and accumulative effect that developers might not notice, and if they do, they will have difficulties to understand the cause.
This will be improved in Unity 5, with Nvidia PhysX 3:
http://blogs.unity3d.com/2014/07/08/high-performance-physics-in-unity-5/
Your answer
Follow this Question
Related Questions
InvokeRepeating OR Update OR FIxedUpdate? 2 Answers
Jumping randomly doesn't work 2 Answers
Updating at frameRate some fixedUpdated gameObject 1 Answer
How do I fix jitter? 1 Answer
Could Update and FixedUpdate happen at the same time? 1 Answer