- Home /
Why doesn't my collision detection script work when using OnTriggerEnter, however when using OnCollisionEnter it doesn't work at all... Would really like to know.. Thank you in advance
public TimeManager timeManager;
[SerializeField]
private float moveSpeed = 10.0f;
[SerializeField]
private float sideWaySpeed = 2f;
[SerializeField]
private float yVelocity;
//[SerializeField]
//public float jumpHeight = 10f;
Vector3 movement;
Rigidbody rb;
// Start is called before the first frame update
void Start()
{
timeManager = GetComponent<TimeManager>();
rb = GetComponent<Rigidbody>();
GameMaster.doublePickedUp = false;
GameMaster.magnetPickedUp = false;
}
// Update is called once per frame
void Update()
{
movement = Vector3.zero;
movement.x = Input.GetAxis("Horizontal") * sideWaySpeed;
if (Input.GetMouseButton(0))
{
if (Input.mousePosition.x > Screen.width / 2)
movement.x = sideWaySpeed;
else
movement.x = -sideWaySpeed;
}
movement.y = yVelocity;
movement.z = moveSpeed;
if (transform.position.y <= -1f)
{
this.enabled = false;
FindObjectOfType<GameMaster>().EndGame();
}
}
void FixedUpdate()
{
MovePlayer(movement);
}
void MovePlayer(Vector3 direction)
{
rb.AddForce(direction * moveSpeed);
}
/*private void OnCollisionEnter(Collision other)
{
if (other.collider.tag == "Obstacle")
{
this.enabled = false;
FindObjectOfType<GameMaster>().EndGame();
}
}*/
void OnTriggerEnter(Collider other)
{
/*if (other.CompareTag("Obstacle"))
{
this.enabled = false;
FindObjectOfType<GameMaster>().EndGame();
}*/
if (other.CompareTag("Coin") && !GameMaster.doublePickedUp)
{
Destroy(other.gameObject);
GameMaster.points += 1;
}
if (other.CompareTag("Coin") && GameMaster.doublePickedUp)
{
Destroy(other.gameObject);
GameMaster.points += 2;
}
if (other.CompareTag("RedCoin"))
{
Destroy(other.gameObject);
GameMaster.points += 5;
}
}
public void SetSpeed(float modifier)
{
moveSpeed = 10.0f * modifier;
}
Answer by BBIT-SOLUTIONS · Apr 06, 2020 at 03:33 AM
When your Collider is marked as isTrigger
(in the inspector) you can use OnTriggerEnter else you can use OnCollisionEnter
Answer by MI_Inc · Apr 06, 2020 at 05:53 AM
I did that, I disabled isTrigger, however the same problem still happens
What exactly is not working? Could you maybe describe the problem a bit more detailed?
No problem man, I figured it out. Im using the built in character controller - it already had a built in collider and everything in it, but thanks for the help
Your answer
Follow this Question
Related Questions
move camera when it collides with a trigger 1 Answer
Player object falling through game environment, collision not detected with ground 0 Answers
Smooth Collisions 2D 0 Answers
Deteching collsions when jumping when not using a rigidbody 2 Answers
freeze an object for a while or reset value or reverse it? 2 Answers