Question by
afpaiva · Dec 21, 2019 at 09:15 PM ·
triggertransform.positionteleport
transform position not working on trigger
I don't know why this code doesn't work. - It writes "game over" on console; - The inspector shows the correct values for Vector3 startpoint; - Tag name is ok, and the code is on character. But the guy does't go to the startpoint... Can someone help me please? Thanks.
public class CharTrigger : MonoBehaviour
{
public GameObject[] helmet;
public GameObject helmetonground;
public Vector3 startpoint;
private void Start()
{
helmet[0].SetActive(false);
startpoint = transform.position;
}
private void OnTriggerEnter(Collider col)
{
if (col.gameObject.CompareTag("Item"))
{
helmet[0].SetActive(true);
helmetonground.SetActive(false);
}
if (col.gameObject.CompareTag("Hole"))
{
transform.position = startpoint;
print("game over");
}
}
Comment
If you use a Rigidbody, use rigidbody.position to move the rigidbody.
Answer by afpaiva · Jan 01, 2020 at 10:50 PM
Thank you ceandros. I found the problem, actually it was an update made on march 2019. CharacterController was overriding GameObject position. Going to Edit > Project Settings > Physics, then click on the checkmark "Auto Sync Transforms" the issue was solved.
Your answer
