- Home /
No collision detection when using special abilities
I made a script that would allow the player to speed up after collecting an item. Baically, it multiplies the players speed by 2 when the player presses the button However, the collision detection doesn't seem to work when the player has the speedup ability and passes through walls.
public class Red_PickUp : PickUp
{
public float boostTime = 2.0f;
public float multiplySpeed = 10.0f;
private float timer;
private float speedUp;
private bool used;
new void Start ()
{
base.Start ();
used = false;
timer = boostTime;
speedUp = player.moveSpeed * multiplySpeed;
}
void Update ()
{
SpeedUp ();
if (used)
{
timer -= Time.deltaTime;
Debug.Log (timer);
player.moveSpeed = speedUp;
if (timer <= 0)
{
timer = 0.0f;
player.moveSpeed /= multiplySpeed;
}
}
}
void SpeedUp ()
{
if (player.collected.Contains (this.gameObject))
{
if (Input.GetKeyUp (KeyCode.Z)) {
used = true;
}
}
}
}
I don't exactly grasp your logic flow; it seems a little dangerous. That said, I don't see anything that would break physics.
Are you perhaps increasing your speed so much that objects are ghosting through colliders? This occurs when you apply a force or request a translation that would move the player a distance greater than the thickness of an obstacle. In such instances, (like with fast-moving projectiles) it's often necessary to supplement the physics sim with some raycasting checks to ensure you don't zip right through walls.
The players speed is set to three and $$anonymous$$ultiplySpeed is set to 10, so the speed becomes 30? that doesn't seem like it makes it unreasonably fast. my walls are cubes that are scaled by 1 in thickness. So, would making my walls thicker make a difference. How would I work with this if I imported models? Do you have any ideas for how I can make the logic more reasonable. Basically, when this object is picked up and the player presses the button, the speedup occurs for 2 seconds.
A speed of 30 units per second is somewhat outrageous if your average obstacle is 1 unit thick. (In a single physics step, your player can move more than 1 unit. This is a recipe for this exact issue).
I suspect this "ghosting" or penetration error is to blame for your troubles. This is a well-known phenomenon for which there are many potential solutions. (Google is your friend).
To some degree, you must limit the mechanics of your game to comply with what the physics simulation is capable of. That said, there are settings at your disposal which can improve the stability and accuracy of the simulation at the cost of performance. See the documentation regarding physics and its associated settings.
Regarding your logic, I suppose if it works, it's fine. I am just picky and particular about how I approach problems. You'll develop stronger practices as you learn.
Regarding your pickup that last for two seconds, it seems like an ideal opportunity to learn about Coroutines. But here again, if it works, just leave it be and you'll pick that topic up when you're ready.
Thanks a lot! :) I'll try slowing him down a bit (maybe a speed of 15 is more reasonable?). Amd I'll look into corputines when I go back and optimize things.
Your answer
Follow this Question
Related Questions
Not detecting collision? 1 Answer
Child Collides If Its The Parents Collider 0 Answers
Check for collision while animating 0 Answers
Destroying object on collision 3 Answers