Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

js to c# | and help with a resetter

Discussion in 'Scripting' started by Uselchen, Jan 16, 2015.

  1. Uselchen

    Uselchen

    Joined:
    Jul 20, 2014
    Posts:
    11
    Hi there,

    First i have to say im quite new to Unity and programming itself. Im running in some issues with my game.

    To give you a better point of view what the game is about here´s a screenshot:



    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:

    Code (JavaScript):
    1.  #pragma strict
    2. public var moveSpeed = 2.0;
    3. function Update ()
    4.  
    5.     {
    6.      if (Input.GetMouseButton(0)) {
    7.          var targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    8.          targetPos.z = transform.position.z;
    9.          transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
    10.      }
    11. }
    And this is my resetter script so far:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class respawn : MonoBehaviour {
    5.  
    6.  
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         collider2D.isTrigger = true;
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.        
    16.     }
    17.    
    18.     void OnTriggerEnter2D(Collider2D coll)
    19.     {
    20.         if(coll.gameObject.tag == "Player")
    21.         {
    22.             Application.LoadLevel(Application.loadedLevel);
    23.            
    24.         }
    25.     }
    26. }
    __________________________
    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 :D (german or english)

    thx in advance
     
  2. Uran

    Uran

    Joined:
    Jul 22, 2012
    Posts:
    8
    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.

    Code (CSharp):
    1. public static class GameEvents
    2. {
    3.     public static event EventHandler<EventArgs> PlayerDeath;
    4.  
    5.     public static void OnPlayerDeath()
    6.     {
    7.         if (PlayerDeath != null)
    8.             PlayerDeath(null, EventArgs.Empty);
    9.     }
    10. }
    11.  
    12. public class Obstacle: MonoBehaviour
    13. {
    14.     private void OnTriggerEnter2D(Collider2D coll)
    15.     {
    16.         if (coll.gameObject.tag == "Player")
    17.             GameEvents.OnPlayerDeath();
    18.     }
    19. }
    20.  
    21. /// <summary>
    22. /// Some object that needs resetting when the player dies.
    23. /// </summary>
    24. public class SomeObject : MonoBehaviour
    25. {
    26.     private void Start()
    27.     {
    28.         GameEvents.PlayerDeath += PlayerDeath;
    29.     }
    30.  
    31.     /// <summary>
    32.     /// This is where you should reset the properties.
    33.     /// </summary>
    34.     /// <param name="sender">This is null.</param>
    35.     /// <param name="e">This is unused.</param>
    36.     private void PlayerDeath(object sender, EventArgs e)
    37.     {
    38.  
    39.     }
    40. }
    41.  

    Character Class Translation
    Code (CSharp):
    1. public class Character : MonoBehaviour
    2. {
    3.     public float moveSpeed = 2.0f;
    4.     private void Update()
    5.     {
    6.         if (Input.GetMouseButton(0))
    7.         {
    8.             Vector3 targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    9.             targetPos.z = transform.position.z;
    10.             transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
    11.         }
    12.     }
    13. }
    Hope this helps :)
     
  3. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    The translation will be something like :

    Code (CSharp):
    1. // PuckMover.cs
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class PuckMover : MonoBehaviour
    7. {
    8.     [SerializeField] private float moveSpeed = 2.0f;
    9.  
    10.     private void Update()
    11.     {
    12.         if (Input.GetMouseButton(0)) // left click
    13.         {
    14.             Vector3 targetPos =
    15.                 Camera.main.ScreenToWorldPoint(Input.mousePosition);
    16.             targetPos.z = transform.position.z;
    17.             transform.position = Vector3.MoveTowards(transform.position,
    18.                                                      targetPos,
    19.                                                      moveSpeed * Time.deltaTime);
    20.         }
    21.     }
    22. }

    I think you will need to rethink the timing of your procedures for the restart.
    Draw out on some paper the sequence

    Hint: why are you re-spawning before the PUC dies a glorious death ??

    added:
    Ooops, Uran snuck in while I was thinking about spellchecking :)
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,200
    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.
     
  5. Uran

    Uran

    Joined:
    Jul 22, 2012
    Posts:
    8
    A bit off topic
    The yellow C# book is free as pdf. As far as I know it should be a fine introduction to C# programming.
     
  6. Uselchen

    Uselchen

    Joined:
    Jul 20, 2014
    Posts:
    11
    Thank you verry much, that helped me out a lot! The GameEvents code you posted to resett everything overwealmed me a little bit :D
    Dont realy know how to use it. Do i combine it with the PUC controlls? or where do i attach this?
     
  7. Uran

    Uran

    Joined:
    Jul 22, 2012
    Posts:
    8
    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.
     
    Last edited: Jan 16, 2015