beginner question - c#

[Problem SOLVED! big thx to Polymorphik]

Hi there Unity Community :slight_smile:

Have a little problem that need to to be solved.
To get a better understanding for the Game here´s a quick link with the early build (playale in browser)

When the PUC hits the wall or an obstacle i want to prevent it from moving further on. Because at the moment if you hit a wall/obstacle you still can move on for 2sec untill the scene resets.

PUC (charakter) control Script:

using UnityEngine;
using System.Collections;

public class puc_controller : MonoBehaviour
{
    public float moveSpeed = 25.0f;
    public AudioClip menuEnterLevel;
    Animator anim;



    void Start () {
     
        anim = GetComponent<Animator> ();

    }


    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);
        }
    }

    void OnTriggerEnter2D(Collider2D coll)
    {
        if(coll.gameObject.tag == "Respawn")
        {
         
            audio.PlayOneShot(menuEnterLevel);
            anim.SetBool("die", true);
          
         
        }
    }
}

Reset Script:
```csharp
**using UnityEngine;
using System.Collections;

public class respawn : MonoBehaviour {

//public AudioClip menuEnterLevel;


IEnumerator death(float delay, int level)
{
    yield return new WaitForSeconds(delay);
    Application.LoadLevel(Application.loadedLevel);
}

void Start () {


    collider2D.isTrigger = true;
}


void Update () {


    
}

void OnTriggerEnter2D(Collider2D coll)
{
    if(coll.gameObject.tag == "Player")
    {

        
        StartCoroutine(death(1.0f,1));
        
        
    }
}

}**
```

Create a method in the controller to disable the script or use a bool to evaluate if the player can move. When you are responding have it toggle so the player can no longer move until it has respawned. You can use

coll.gameObject.SendMessage(“Method”, SendMessageOptions.DontRequireReceiver);

Thanks for you answer, sounds logic to me, but as i said im quite new to Unity and Programming, i understand what you mean but dont know how to use i mean where and in which script to put this :smile:

Controller

using UnityEngine;
using System.Collections;

public class puc_controller : MonoBehaviour
{
    public float moveSpeed = 25.0f;
    public AudioClip menuEnterLevel;
    Animator anim;
    bool canMove = true;
   
   
    void Start () {
        anim = GetComponent<Animator> ();
    }
   
   
    private void Update()
    {
        if (Input.GetMouseButton(0) && this.canMove == true)
        {
            Vector3 targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            targetPos.z = transform.position.z;
            transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
        }
    }
   
    void OnTriggerEnter2D(Collider2D coll)
    {
        if(coll.gameObject.tag == "Respawn")
        {
           
            audio.PlayOneShot(menuEnterLevel);
            anim.SetBool("die", true);
        }
    }

    void ToggleMove(bool canMove) {
        this.canMove = canMove;
    }
}

Respawn

using UnityEngine;
using System.Collections;

public class respawn : MonoBehaviour {
   
    //public AudioClip menuEnterLevel;
   
   
    IEnumerator death(float delay, int level)
    {
        yield return new WaitForSeconds(delay);
        Application.LoadLevel(Application.loadedLevel);
    }
   
    void Start () {
        collider2D.isTrigger = true;
    }
   
   
    void Update () { }
   
    void OnTriggerEnter2D(Collider2D coll)
    {
        if(coll.gameObject.tag == "Player")
        {
            StartCoroutine(death(1.0f,1));
            coll.gameObject.SendMessage("ToggleMove", false, SendMessageOptions.DontRequireReceiver);
        }
    }
}

I’m assuming after you restart, it reloads everything all over again. If the controller persists from scene to scene then we need to change the logic. But I don’t see it here so this should do it.

Hey Poly, thank you sooo much for the code. Everythihing works as intended now! Love it :slight_smile:

Next Challange “little” nice to have gimmiks, screenshake, particle effects on death, ect :slight_smile: But i guess i should be able to figure that out.