topical media & game development

talk show tell print

lib-unity-tutorial-m2h-2-Assets-Game-1-Catch-eggs-Scripts-PlayerScript.cs / cs



  using UnityEngine;
  using System.Collections;
  
  public class PlayerScript : MonoBehaviour {
      
      public int theScore = 0;
  
          void Update () {
          //These two lines are all there is to the actual movement..
          float moveInput = Input.GetAxis("Horizontal") * Time.deltaTime * 3; 
          transform.position += new Vector3(moveInput, 0, 0);
  
          //Restrict movement between two values
          if (transform.position.x <= -2.5f || transform.position.x >= 2.5f)
          {
              float xPos = Mathf.Clamp(transform.position.x, -2.5f, 2.5f); //Clamp between min -2.5 and max 2.5
              transform.position = new Vector3(xPos, transform.position.y, transform.position.z);
          }
          }
  
      
      void OnGUI()
      {
          //We display the game GUI from the playerscript
          //It would be nicer to have a seperate script dedicated to the GUI though...
          GUILayout.Label("Score: " + theScore);
      }    
  }
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.