Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerController : MonoBehaviour {
- public float speed; //Floating point variable to store the player's movement speed.
- private Rigidbody2D rb2d; //Store a reference to the Rigidbody2D component required to use 2D Physics
- void Start()
- {
- rb2d = GetComponent<Rigidbody2D> (); //Get and store a reference to the Rigidbody2D component so that we can access it.
- }
- void FixedUpdate() //FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
- {
- float moveHorizontal = Input.GetAxis ("Horizontal"); //Store the current horizontal input in the float moveHorizontal.
- float moveVertical = Input.GetAxis("Vertical");
- //grabs the input from player thru keyboard; float var moves hor/ver and record the hor/ver axis by keyboard
- Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
- //Use the two store floats to create a new Vector2 variable movement.
- rb2d.AddForce (movement * speed);
- //Call the AddForce function of our Rigidbody2D rb2d supplying movement multiplied by speed to move our player.
- }
- void OnTriggerEnter2D(Collider2D other) //OnTriggerEnter2D is called whenever this object overlaps with a trigger collider.
- {
- if (other.gameObject.CompareTag("PickUp")) //Check the provided Collider2D parameter other to see if it is tagged "PickUp", if it is...
- {
- other.gameObject.SetActive(false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement