- Home /
Player sprite sinks into colliders
I've got a scene there there are 3 layers of 30 objects, each with their own box colliders. The way the "game" works atm is when the player presses A S or D if the sprite is next to a object, the "digs" through and removes it. The issue comes in when the sprite is on the second layer and trying to dig through the sides of an object. The sprite seems to sink into one of the objects below and destroys them instead
I'll link a video to show it in action, maybe it will be easier to see it happening: https://youtu.be/5iTmIBQrc4w
If anyone can suggest possible solutions, that would be great
EDIT:
Player movement
void Update () {
float axisX = Input.GetAxis ("Horizontal");
float axisY = Input.GetAxis ("Vertical");
transform.Translate (new Vector3 (axisX, axisY) * Time.deltaTime * speed);
//Speed is set to 10
}
Object removal
void OnCollisionStay2D(Collision2D collStay)
{
if (isTriggered == false && Input.GetKey ("s")){
countDown -= Time.deltaTime;
print (countDown);
if (countDown <= 0) {
dirtCount = dirtCount + 1;
print (dirtCount);
Destroy (collStay.gameObject);
countDown = 2.0f;
}
}
if (enteredBlockSide == true && Input.GetKey ("d")){
countDown -= Time.deltaTime;
print (countDown);
if (countDown <= 0) {
dirtCount = dirtCount + 1;
print (dirtCount);
Destroy (collStay.gameObject);
countDown = 2.0f;
}
}
if (enteredBlockSide == true && Input.GetKey ("a")){
countDown -= Time.deltaTime;
print (countDown);
if (countDown <= 0) {
dirtCount = dirtCount + 1;
print (dirtCount);
Destroy (collStay.gameObject);
countDown = 2.0f;
}
}
}
isTriggered is there to stop removal of 2 objects at once. enteredBlockSide is a Linecast which I was hoping would solve my issue (it didn't).
Hey!
Could you provide some code, about the player movement and how digging direction is decided? Based on the video, I think your player movement might conflict with physics. Also, collisions are not precise, so if you want to decide in which layer to dig using player position, you should use a safety limit.
A different solution is to have a separate, smaller collider for the drill, and move it to the digging direction relative to the player center when the key is pressed. Then ins$$anonymous$$d of checking the collision or position of player, you could check collision with the drill.
Code has been added. Let me know if there's anything else you need. Thanks for the help
Answer by Harinezumi · May 01, 2018 at 09:03 AM
Great, thanks for providing code and explanation.
I think what happens is that OnCollisionStay2D()
for the block in the lower layer is executed before it is executed for the upper layer, so it gets destroyed, then countDown
is already > 0 (if it is static variable or in the same script) so it doesn't destroy the current layer. I'm not sure why the line cast doesn't help, but it must be true for both blocks. Is enteredBlockSide
a static variable or in the same script?
I'm not sure if you have this logic on the player or on the blocks (both can make sense), but I would put it on the blocks, but set in the player the block being dug (with the Linecast from the centre of the player, ignoring the player's layer), and then check if this block is the same as the player has selected. Does this make sense?
Also, as your player object has a collider, I would suggest to not move it by transform.Translate()
, but by rigidbody.MovePosition()
. See the answer to this question for why (aside that it is better performance-wise).
Btw, you could move the repetitive if (countDown <= 0)
part into a separate function (for example you could call it Dig()
), so that if you want to change it you only need to do it in one place.
Thanks for the answer, I'll give this a go
EDIT: the logic is on the player and the linecast is in the same script just in the update function
Your answer
Follow this Question
Related Questions
Per-Sorting Layer Collision 1 Answer
Arrays? ↓↓↓↓↓↓↓ 2 Answers
2D project with movement along Z and X? 1 Answer
Spawn gap in unity? 0 Answers
Sprites and level tiles overlapping when they shouldn't 0 Answers