Hey Guys,
I’m working rightnow on the Checkpointsystem that I use for a platformer and it’s working quite good except for one issue.
I have an object with a collider trigger statement that is used as obstacle. The thing is it doesn’t set the player back to the last checkpoint the player was hitting.
I have a fall detection in it and on a key for start at the last checkpoint, both with the same code part in it. Both systems working without any problem.
I compared both things and what I need is that the Update function needs an if-statement that tells that the player is hitting a certian collider (tagged as “Trap”) and then set him back to the last checkpoint.
using UnityEngine;
using System.Collections;
public class Death : MonoBehaviour
{
void Start()
{
GameVariables.checkpoint = new Vector3 ();
}
void Update()
{
if( //check if Player hit the tagged "Trap" collider )
transform.position = GameVariables.checkpoint;
}
}
to note. If I put the transform.positon thing to an OntriggerEnter function nothing happens.
if you want to do something when the player collides with a trigger collider you need to do that in the OnTriggerEnter(…) function.
Show us how you tried that when “nothing happens” and we’ll correct that.
It’s only
public class Death : MonoBehaviour
{
void Start()
{
GameVariables.checkpoint = new Vector3 ();
}
void Update()
{
}
void OntriggerEnter(Collider other)
{
if (other.tag == "Player")
{
transform.position = GameVariables.checkpoint;
}
}
}
the other codes that’re working with the Checkpoints are called in the Update function. So I thought that will be the answer to it.
Like the above poster said, you need to use the engine provided collision detection methods for this. Try using the method below in your code. I’m assuming this is attached to the player.
void OnTriggerEnter (Collider other) {
if (other.gameObject.tag == "TRAP") {
transform.position = GameVariables.checkpoint;
}
}
OnTriggerEnter
not
OntriggerEnter
also something in the collision needs to have a rigidbody component on it otherwise the physics engine isn’t going to be aware
I really thought for a moment that my mistake with the little t instead of the big T were the problem:face_with_spiral_eyes:…but it was not. -.-
I’ll show you the one that’s working.
void Update()
{
// some code stuff
if (transform.position.y < -10)
{
transform.position = GameVariables.checkpoint;
}
if (Input.GetKey (KeyCode.R) || Input.GetKey (KeyCode.Joystick1Button3))
{
transform.position = GameVariables.checkpoint;
}
}
is the Death script attached to the player or the obstacle?
Obstacle. Yes and has a tag “Trap”
in which case it resets the position of the obstacle. lowercase transform is a “this transform” reference.
If you want to reset the player you need to use
other.transform.position =
or a better approach would be to have give player script a function which handles “respawn me” and call that instead for all cases where you want the player to respawn.
haha!! that other.transform.position = worked.
sometime is coding really … you know…sometime…
Anyway thank you Guys for your help:smile:
“just” need to be aware of what the references are referring to