nstruth2

PlayerController File

Feb 18th, 2024
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour
  6. {
  7.  
  8.     public delegate void OnCollectBallAction();
  9.     public OnCollectBallAction OnCollectBall;
  10.     float speed = 600.0f;
  11.     public Camera mainCamera;
  12.     Vector3 leftBound;
  13.     Vector3 rightBound;
  14.  
  15.     void Start()
  16.     {
  17.       leftBound = mainCamera.ViewportToWorldPoint(new Vector3(0,1, -mainCamera.transform.localPosition.z));
  18.       rightBound = mainCamera.ViewportToWorldPoint(new Vector3(1, 0, -mainCamera.transform.localPosition.z));
  19.  
  20.     }
  21.  
  22.  
  23.     void Update()
  24.     {
  25.         keepInBounds();
  26.         processInput();
  27.         lockPlayerZPosition();
  28.     }
  29.  
  30.     void lockPlayerZPosition()
  31.     {
  32.         this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, 0);
  33.     }
  34.  
  35.     void keepInBounds()
  36.     {
  37.         if(this.transform.position.x < leftBound.x)
  38.         {
  39.             this.transform.position = new Vector3(leftBound.x, this.transform.position.y, this.transform.position.z);
  40.         }
  41.         if(this.transform.position.y > leftBound.y)
  42.         {
  43.             this.transform.position = new Vector3(this.transform.position.x, leftBound.y, this.transform.position.z);
  44.         }
  45.         if(this.transform.position.x > rightBound.x)
  46.         {
  47.             this.transform.position = new Vector3(rightBound.x, this.transform.position.y, this.transform.position.z);
  48.         }
  49.         if(this.transform.position.y < rightBound.y)
  50.         {
  51.             this.transform.position = new Vector3(this.transform.position.x, rightBound.y, this.transform.position.z);
  52.         }
  53.  
  54.     }
  55.  
  56.     void processInput()
  57.     {
  58.         if(Input.GetKey("left") || Input.GetKey("a"))
  59.         {
  60.             this.GetComponent<Rigidbody>().AddForce(Vector3.left * speed * Time.deltaTime);
  61.         }
  62.         if(Input.GetKey("right") || Input.GetKey("d"))
  63.         {
  64.             this.GetComponent<Rigidbody>().AddForce(Vector3.right * speed * Time.deltaTime);
  65.         }
  66.  
  67.     }
  68.  
  69.     public void OnCollisionEnter (Collision collision)
  70.     {
  71.         if(GetComponent<Collider>().GetComponent<BallController>() != null)
  72.         {
  73.             if(OnCollectBall != null)
  74.             {
  75.                 OnCollectBall();
  76.             }
  77.             GameObject.Destroy(GetComponent<Collider>().gameObject);
  78.         }
  79.     }
  80.  
  81. }
  82.  
Add Comment
Please, Sign In to add comment