The goal is it to drag the player (red puc) to the finish line. (dragged with mouse cursor)
The blue obstacle (saw) is rotating in 360°
Now i need a propper way to reset my scene on death (collision with obsacle or level borders)
1st - What i want to do is reset my scene on death but not reload the whole scene so i can add a “die” sound and annimation. But the only way to reset the level i found out so far is to reset the whole scene which prevents me from adding a death sound/annimation.
2nd - My Charaktercontrollerscript is in .js the most of my other code in c# so i guess it would be better to translate the charakter script in c# too but i have no idea how.
This is my charakter script:
#pragma strict
public var moveSpeed = 2.0;
function Update ()
{
if (Input.GetMouseButton(0)) {
var targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
targetPos.z = transform.position.z;
transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
}
}
And this is my resetter script so far:
using UnityEngine;
using System.Collections;
public class respawn : MonoBehaviour {
// Use this for initialization
void Start () {
collider2D.isTrigger = true;
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D coll)
{
if(coll.gameObject.tag == "Player")
{
Application.LoadLevel(Application.loadedLevel);
}
}
}
__________________________
So what i need now is help to convert the js script to c# and a propper way to resett the objects and timer in my scene to its initial state. I guess a way would be to have the gameObjects initial positions/rotations in a function and call this function on collision (death) ?
I dont realy know if thats the best way to do it. If there is any better way please help me
I would realy apprechiate help via Skype or TeamViewer if anyone have some spare time (german or english)
Resetting objects
I would have a property for each value that need to reset on death, then an event that is triggered when the player dies.
public static class GameEvents
{
public static event EventHandler<EventArgs> PlayerDeath;
public static void OnPlayerDeath()
{
if (PlayerDeath != null)
PlayerDeath(null, EventArgs.Empty);
}
}
public class Obstacle: MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D coll)
{
if (coll.gameObject.tag == "Player")
GameEvents.OnPlayerDeath();
}
}
/// <summary>
/// Some object that needs resetting when the player dies.
/// </summary>
public class SomeObject : MonoBehaviour
{
private void Start()
{
GameEvents.PlayerDeath += PlayerDeath;
}
/// <summary>
/// This is where you should reset the properties.
/// </summary>
/// <param name="sender">This is null.</param>
/// <param name="e">This is unused.</param>
private void PlayerDeath(object sender, EventArgs e)
{
}
}
Character Class Translation
public class Character : MonoBehaviour
{
public float moveSpeed = 2.0f;
private void Update()
{
if (Input.GetMouseButton(0))
{
Vector3 targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
targetPos.z = transform.position.z;
transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
}
}
}
I’d just play the death animation and sounds and whatever, and then reload the level. Use a coroutine to wait until the anim and sound is done, and call a normal Application.LoadLevel. If you need a respawn to be different from a normal level start, just store the fact that you’re doing a respawn in PlayerPrefs, and react to that on scene load.
If you move things instead of resetting the level, you’ll have to put reset stuff into every. single. script. You also have to put scripts on everything that can move. You don’t want to have to do that.
Thank you verry much, that helped me out a lot! The GameEvents code you posted to resett everything overwealmed me a little bit
Dont realy know how to use it. Do i combine it with the PUC controlls? or where do i attach this?
The GameEvents class is static and therefore don’t need to be attached to a GameObject, or more corectly you can’t attach static classes to GameObjects.
Attach the Character class to the PUC GameObject. The SomeObject class (which is horribly named) is attached to saws and other objects that kills the character when thouched.