Question by
SeanKilleen · Oct 19, 2016 at 09:40 AM ·
2dscripting problemcollision2d-platformer
OnTriggerEnter getting called more than once
Hi All,
I am creating (trying to create) a 2d platformer game. I have a specific type of platform that doubles the players speed when he lands on it. My problem is that the OnTrigger Enter is getting called more than once for the same platform. I have added a boolean to prevent this from happening but surely I shouldn't need this. Can you please tell me why my trigger enter event is getting called more than once.
using UnityEngine;
using System.Collections;
public class SpeedController : MonoBehaviour {
private bool playerIsInTrigger = false;
private PlayerSettings playerSettings;
public bool increase = true;
// Use this for initialization
void Start () {
playerSettings = (PlayerSettings)GameObject.Find("Player").GetComponent("PlayerSettings");
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D other){
if(playerIsInTrigger)
return;
var speedscale = playerSettings.speedScale;
if (increase && speedscale <= 1 ) {
playerSettings.speedScale *= 2f;
}
else if (!increase && speedscale >= 1 ){
playerSettings.speedScale /= 2f;
}
}
void OnTriggerExit2D(Collider2D other){
playerIsInTrigger = false;
}
}
Comment
What kind of player do you have?, like humanoid, some kind of shape or something else. $$anonymous$$aybe it's because the player enter the collider more than once. for eg. a humanoid character will move with his feets which will keep on entering/exiting the Collider, resulting in this behaviour. So, try to check this.