Friday, March 03, 2017

Restart Script: Note the name of the scene in SceneManager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class PlayerController : MonoBehaviour {


    public float speed;
    public Text countText;
    public Text winText;

    private Rigidbody rb;
    private int count;
    private bool grounded = true;

    void Start ()
    {
       
        rb = GetComponent();
        count = 0;
        SetCountText ();
        winText.text = "";
    }
    void FixedUpdate ()

{
      float moveHorizontal = Input.GetAxis ("Horizontal");
      float moveVertical = Input.GetAxis ("Vertical");

       
        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

        rb.AddForce (movement * speed);

        if (Input.GetKeyDown (KeyCode.Space)&&grounded)
        {
                rb.velocity = new Vector3 (rb.velocity.x, 5, rb.velocity.z);
                grounded = false;
            }
        if (Input.GetKey (KeyCode.R))
        {
            //load that level
            SceneManager.LoadScene ("Roll A Ball Mod");
        }


    }
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag ("Pick Up"))
        {
            other.gameObject.SetActive (false);
            count = count + 1;
            SetCountText ();
        }
      }
        void OnCollisionEnter (Collision Other)
        {
            if (Other.gameObject.CompareTag ("Ground"))
            {
                grounded = true;
            }
        }
    void SetCountText ()
    {
        countText.text = "Score :" + count.ToString ();
        if (count >= 12)
        {
            winText.text = " You Win!";
        }
    }

}
  

No comments: