- Home /
Question by
nebulaisrighthere · Aug 09, 2018 at 01:49 AM ·
c#controllercollision detectionnot workingteleporting
Simple Teleportation Confusion
I want when my character touches the plane which I call "Floor" it teleport the player back to the start which is (0, 3, 40); You know floor is lava. This my code:
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.gameObject.name == "Floor")
{
transform.position = new Vector3(0, 3, 40);
}
Rigidbody body = hit.collider.attachedRigidbody;
//dont move the rigidbody if the character is on top of it
if (m_CollisionFlags == CollisionFlags.Below)
{
return;
}
if (body == null || body.isKinematic)
{
return;
}
body.AddForceAtPosition(m_CharacterController.velocity * 0.1f, hit.point, ForceMode.Impulse);
}
When you replace the teleportation code with a debug.log code it notices that the player is on the plane
if (hit.gameObject.name == "Floor")
{
Debug.Log("TouchingFloor");
}
Its wierd..
Comment
Answer by nebulaisrighthere · Aug 17, 2018 at 12:56 PM
Never Mind I did it.
Here the code if anyone else needs it..
private bool Touched;
if (Touched)
{
transform.position = new Vector3(-3, 0, 17);
Touched = false;
Debug.Log("Reset");
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
Rigidbody body = hit.collider.attachedRigidbody;
//dont move the rigidbody if the character is on top of it
if (hit.gameObject.name == "Floor")
{
Touched = true;
}
if (m_CollisionFlags == CollisionFlags.Below)
{
return;
}
if (body == null || body.isKinematic)
{
return;
}
body.AddForceAtPosition(m_CharacterController.velocity * 0.1f, hit.point, ForceMode.Impulse);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612172459im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to hit objects in VR 1 Answer
Multiple Cars not working 1 Answer
transform.Translate causing object to teleport. 3 Answers
Distribute terrain in zones 3 Answers
c# enemy's not spawning 1 Answer