- Home /
Jumping causes strange bugs and messages
Every time i jump the player disappears off the screen and in the inspector the Transform Position Y changes at alarming rates. I also get this message in the Inspector Transform tab - "Due to floating-point precision limitations, it is recommended to bring the world coordinates of the GameObject within a smaller range." What does this mean?
Heres the source code if it helps: using UnityEngine; using System.Collections;
public class Move : MonoBehaviour
{
public float moveSpeed = 7f;
public float turnSpeed = 150f;
public float jumpSpeed = 0.25f;
void Update ()
{
if(Input.GetKey(KeyCode.W))
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.S))
transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.A))
transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.D))
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
//jump test
if (Input.GetKey (KeyCode.Space) && !Gravity.boolGravity) {
rigidbody.AddForce(Vector3.up * jumpSpeed);;
Gravity.boolGravity = true;
}
}
}
What is the mass of the attached rigidbody?
Also, have you tried using 'AddForce' for all of the movement, ins$$anonymous$$d of just jump?
Also also, have you tried moving the 'AddForce' bit to FixedUpdated?
Answer by Atonez · Sep 24, 2014 at 01:52 AM
In your code I see that you have (-Vector3.forward moveSpeed Time.deltaTime), And you have Vector3.up, -turnSpeed * Time.deltaTime)try instead:
Vector3.forward (-moveSpeed Time.deltaTime); Vector3.up, (-turnSpeed * Time.deltaTime);
Answer by Doodayer · Sep 24, 2014 at 02:32 AM
ok so i fixed it by changing my rigidBody mass, but now i need to know how to do one of two things.
I could either use a timer to set boolGravity back to true, or i could make it to where when my players y value reaches a certain value such as 6 or 10 it sets it back to true.
If i do option one, i'll need a lot of help as i have no experience with timers in C# If i do option two, all i need to know is how to use an if statement to access my players y value.
thanks for all the help!
Why not have gravity always on? You could also put a small collider as a trigger at the feet of your player and manage the gravity bool in OnTriggerEnter and OnTriggerExit
With gravity always on, the player begins to dig into the ground object, and eventually either going through, or beginning to levitate and fly in circles.
As i told you, use rigibody.move to prevent that. Tralslate does not tace into account collition thats why it digs into de ground.
so why when i use this code: rigidbody.AddForce(Vector3.down * playerGravity); it makes my player start moving into the ground when i move? that's my main problem is that the collision never stops and he starts going into the ground and then flying when it pops back out.
Answer by LSPressWorks · Sep 25, 2014 at 04:18 AM
They have answered your question like 5 times already. There is seldom a need to add gravity manually. Is there a reason for it?
as for the other
var OldPosition : Vector3;
if(jumping != false)
{
if(Vector3.Distance(oldPosition,newPosition) > value)
{
jumping = false;
}
}
there are about 70,000 methods for that though. Once jumping is false, you can use whatever code you wish once the condition is met.