- Home /
Ground Detection Lagging - 2D
So, I've been trying to find a means of fixing this issue, and I'm finally resorting to this here for some of you more knowledgeable folks.. So, basically I'm working on a 2D game. I've tried several different means of ground detection and the only thing I can find that works is a bit conflicting. I've tried physics2D.CircleOverlap, raycasting, line casting, and even setting up a trigger to tell the PlayerController script when it touches the ground. However, if the player reaches a certain speed, they will sink someway in the ground before stopping. This generally happens once jumping off a platform to a lower surface...
To fix this problem, I've turned "Is Kinematic" off of my player object. However, I'm simulating my own gravity through scripting, and would like to ignore physics for this, and most other objects. Simply putting the Gravity scale to 0 isn't exactly my ideal means of resolving this issue. Any advice at all is much appreciated, also, my jumpScript is much longer than this, but this shows all the movement stuff related to it. If you do need any more information at all, please feel free to ask, I figured a shorter version would be more efficient for whoever is reading ^_^.
Edit: I wrote this up when I was rather tired. I noticed a couple errors in my script, so just updating that.
using UnityEngine;
using System.Collections;
public class TestScript : MonoBehaviour {
private float decreaseTimer;
public float decrease;
public GameObject bottom;
private bool jumping;
private Vector3 bottomVec;
public float jumpHeight;
// Update is called once per frame
void FixedUpdate () {
if(Input.GetKey (KeyCode.Space)){
jumping = true;
//IfInputJumpisTrue
}
if(IsGrounded() == false){
decreaseTimer += Time.deltaTime;
//TimeLengthOfJump-MultipleSubtractionByThis
}
if(IsGrounded()){
decreaseTimer = 0f;
jumping = false;
//ResetTimer
}
if(jumping){
transform.Translate(0f, jumpHeight - (decrease * decreaseTimer), 0f);
//WillEventuallyReturnNegative
}
}
private bool IsGrounded(){
bottomVec = bottom.transform.position;
return Physics2D.Linecast(transform.position, new Vector3(bottomVec.x ,bottomVec.y), 1 << LayerMask.NameToLayer("Ground"));
//LineCast,CheckForGround
}
}
Answer by Aggrojag · Jun 28, 2014 at 06:40 AM
So, after digging through Unity, I believe I found the answer to my question. When Is Kinematic is set to false, unity will check to see if the object is inside of another object. If it is, it will offset the value of the transform until it is outside of the object. I would provide sample script to get around this if I knew how, so my apologies that this is not provided! Cheers!
Answer by grendayzer77 · Feb 08, 2016 at 11:31 AM
i had the same problem and i've set Collision Detection to Continuous instead of Discrete, and it solved the problem.
Your answer
Follow this Question
Related Questions
2D C# Jump Script Addforce 2 Answers
Distribute terrain in zones 3 Answers
Help With Simple Jump Script 1 Answer
GetButtonDown not always firing 1 Answer