Reputation: 47
I want my Player to collide with the object capsule. This action should destroy the capsule and add a speed value of 10 to the player.
But this code is not working :
public class PlayerController : MonoBehaviour {
public KeyCode moveL;
public KeyCode moveR;
public float horizontal = 0;
public int laneNum = 2;
public string controllocked = "n";
public float speed;
void Update ()
{
GetComponent<Rigidbody>().velocity = new Vector3(horizontal, GM.verticalVelocity, speed);
if ((Input.GetKeyDown(moveL)) && (laneNum > 1) && (controllocked == "n"))
{
horizontal = -2;
StartCoroutine(StopSlide());
laneNum = laneNum - 1;
controllocked = "y";
}
else if ((Input.GetKeyDown(moveR)) && (laneNum < 3) && (controllocked =="n"))
{
horizontal = 2;
laneNum = laneNum + 1;
StartCoroutine(StopSlide());
controllocked = "y";
}
}
void OnCollisionEnter(Collision other)
{
if(other.gameObject.tag == "lethal")
{
Destroy(gameObject);
}
if (other.gameObject.name == "Capsule")
{
Destroy(other.gameObject);
speed = 10;
}
}
IEnumerator StopSlide()
{
yield return new WaitForSeconds(.5f);
horizontal = 0;
controllocked = "n";
}
What I've tried so far is speed += 10
and speed++
neither works.
Upvotes: 1
Views: 4138
Reputation: 226
Well, at first try to check your player, What collider type that you use in the player? Make sure you check trigger in the collider component and add rigidbody into it. The capsule object must have rigidbody on it. Hope it help. Use OnTriggerEnter(Collider collider) if you want to use triggerenter. Collision enter work if trigger is not checked
Upvotes: 2