Advertisement
Guest User

UFO Game

a guest
Mar 25th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.58 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour {
  6.  
  7.     public float speed;       //Floating point variable to store the player's movement speed.
  8.  
  9.     private Rigidbody2D rb2d;     //Store a reference to the Rigidbody2D component required to use 2D Physics
  10.  
  11.     void Start()
  12.     {
  13.         rb2d = GetComponent<Rigidbody2D> (); //Get and store a reference to the Rigidbody2D component so that we can access it.
  14.     }
  15.  
  16.     void FixedUpdate()  //FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
  17.     {
  18.         float moveHorizontal = Input.GetAxis ("Horizontal"); //Store the current horizontal input in the float moveHorizontal.
  19.         float moveVertical = Input.GetAxis("Vertical");
  20.         //grabs the input from player thru keyboard; float var moves hor/ver and record the hor/ver axis by keyboard
  21.  
  22.  
  23.         Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
  24.         //Use the two store floats to create a new Vector2 variable movement.
  25.  
  26.         rb2d.AddForce (movement * speed);
  27.         //Call the AddForce function of our Rigidbody2D rb2d supplying movement multiplied by speed to move our player.
  28.     }
  29.  
  30.     void OnTriggerEnter2D(Collider2D other) //OnTriggerEnter2D is called whenever this object overlaps with a trigger collider.
  31.     {
  32.         if (other.gameObject.CompareTag("PickUp")) //Check the provided Collider2D parameter other to see if it is tagged "PickUp", if it is...
  33.         {
  34.                     other.gameObject.SetActive(false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement