Reset GameObject with trigger on respawn
Hey guys and girls. So I made a trap. The trap is a sprite that is a weight, to this weight I have attached my "hurtPlayer" script, which works fine.
On the bottom I have my spring board with a script that makes the weight fall down and then hurt the player. The board on the bottom has a boxColldier2D with a trigger. I have to attach my weight gameObject to the script on the sprinboard to make it work.
I also have a resetOnRespawn script that literally works for all my enemies and even this weight. However what happens is when I respawn the trigger to the weight is activated again and the weight falls right when I am respawning. Is there a way to fix this, so it doesn't just fall when I am respawning?
CODE FOR TRAP: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class FallingTraps : MonoBehaviour {
public GameObject fallingTrap;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "Player") {
//GameObject.FindWithTag ("FallingTrap").GetComponent<Rigidbody2D> ().isKinematic = false;
fallingTrap.GetComponent<Rigidbody2D> ().isKinematic = false;
}
}
}
And RESET ON RESPAWN SCRIPT: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ResetOnRespawn : MonoBehaviour {
private Vector3 startPosition;
private Quaternion startRotation;
private Vector3 startLocalScale;
private Rigidbody2D myRigidBody;
// Use this for initialization
void Start () {
startPosition = transform.position;
startRotation = transform.rotation;
startLocalScale = transform.localScale;
if(GetComponent<Rigidbody2D>() != null){
myRigidBody = GetComponent<Rigidbody2D> ();
}
}
// Update is called once per frame
void Update () {
}
public void ResetObject(){
transform.position = startPosition;
transform.rotation = startRotation;
transform.localScale = startLocalScale;
if (myRigidBody != null) {
myRigidBody.velocity = Vector3.zero;
}
}
}
Answer by Commoble · Jan 28, 2018 at 08:25 PM
When you activate the trap, you're activating it by setting its rigidbody.isKinematic
to false, so you'll need to set it back to true when you reset it.