- Home /
Buggy Collision
I'm trying to make a game where the player can move in 4 directions, up, down, left, and right. My game is basically a slider puzzle. I've managed to make it work, however when it comes to collision with other objects the game bugs a bit. I'll be using a box collider, using a cube as the player and obstacles, and if i move up and just happen to brush aside another none moving cube the player stops either on the beggning of its edge or halfway through it. I've changed the collider from a box to sphere and this friction doesn't happen as much, however it still exists. I've tried putting a material on it and setting it's friction to 0 and it still continues. I have no ideia how to fix this, what can I do now?
This is my code
Blockquote
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PcTest : MonoBehaviour { // Start is called before the first frame update public float x, y, speed = 20f, pressed, pres1 = 1f, pres2 = 1f, pres3 = 1f, pres4 = 1f, distance = 1f; public bool up, left, down, right, moving, limitup, limitdown, limitleft, limitright, collided, clicked, stopped, cantup, cantdown, cantright, cantleft, tester; public Rigidbody rb; public Vector2 vel; public float timer = 1f; public float lastx, lasty; public bool elevator; public int counter; public GameObject door2; public bool coli; public Vector3 stopPos; // Use this for initialization void Start() { stopped = true; rb = GetComponent(); lastx = transform.position.x; lasty = transform.position.y; }
// Update is called once per frame
void Update()
{
if (coli)
{
counter += 1;
coli = false;
}
if (counter >= 2) { Destroy(door2); }
x = Mathf.Round(rb.velocity.x * 100) / 100;
y = Mathf.Round(rb.velocity.y * 100) / 100;
if (!moving)
{
if (Input.GetKey("up") && !cantup )
{
rb.isKinematic = false;
down = false;
up = true;
left = false;
right = false;
stopped = false;
moving = true;
}
else
if (Input.GetKey("down") && !cantdown )
{
rb.isKinematic = false;
down = true;
up = false;
left = false;
right = false;
stopped = false;
moving = true;
}
else
if (Input.GetKey("right") && !cantright )
{
rb.isKinematic = false;
down = false;
up = false;
left = false;
right = true;
stopped = false;
moving = true;
}
else
if (Input.GetKey("left") && !cantleft )
{
rb.isKinematic = false;
down = false;
up = false;
left = true;
right = false;
stopped = false;
moving = true;
}
}
if (!stopped )
{
if (up && !cantup)
{
// rb.velocity = new Vector3(0, 15, 0);
rb.AddForce(transform.up * speed);
rb.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationZ
| RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationX;
}
if (down && !cantdown)
{
// rb.velocity = new Vector3(0, -15, 0);
rb.AddForce(-transform.up * speed);
rb.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationZ
| RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationX;
}
if (right && !cantright)
{
rb.AddForce(transform.right * speed);
// rb.velocity = new Vector3(15, 0, 0);
rb.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationZ
| RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationX;
}
if (left && !cantleft)
{
rb.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationZ
| RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationX;
rb.AddForce(-transform.right * speed);
// rb.velocity = new Vector3(-15, 0, 0);
}
}
if (x == 0f && y == 0f)
{
stopped = true;
moving = false;
down = false;
up = false;
left = false;
right = false;
clicked = false;
}
else { stopped = false; }
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Player")
{
rb.isKinematic = true;
//rb.velocity = Vector3.zero;
stopped = true;
moving = false;
down = false;
up = false;
left = false;
right = false;
clicked = false;
}
if (col.gameObject.tag == "key")
{
right = false;
left= false;
up = false;
down = false;
}
}
}
Answer by Major · Mar 10, 2019 at 07:06 AM
This should address your issues.
I expressed my self incorrently, sorry about that. By slider i meant like an ice puzzle so no overlapping. $$anonymous$$y issue is the friction of the objects, whenever i try to go up he sort of collides with it like so
From your code it would seem that you zero the player's velocity when a collision occurs. Even just brushing up against another collider can still cause a collision to exist, ergo your player will stop moving when it touches another collider. What I proposed would still work for you, you would just have to search a little farther in each direction, rather than just 1 tile.
If that doesn't work for you, then you might consider ray casting in the direction of movement to deter$$anonymous$$e if the movement direction is viable.
I deactivated the zero velocity code, all though It's much better than what it was it's still colliding with the edges from time to time. I tried using rb.velocity and add force with an INT and he stills collides on the edges from time to time, even with every object on the scene with 0 friction. As for the raycast I used it in the beggining but I don't see how it could avoid this friction from happening. Either way you helped a lot already, thanks!