Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.01 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour{
  6.     [SerializeField] float speed = 8f;
  7.     float bulletspeed = 10f;
  8.     float xMin = 0f;
  9.     float xMax = 0f;
  10.     float yMin = 0f;
  11.     float yMax = 0f;
  12.     [SerializeField] Transform SpawnPoint;
  13.     [SerializeField] GameObject Bullet;
  14.     [SerializeField] float offset = 0.5f;
  15.     // Start is called before the first frame update
  16.     void Start()
  17.     {
  18.         CameraPoints();
  19.  
  20.     }
  21.  
  22.     private void CameraPoints()
  23.     {
  24.         Camera cam = Camera.main;
  25.  
  26.         xMin = cam.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + offset;
  27.  
  28.         xMax = cam.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - offset;
  29.  
  30.         yMin = cam.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + offset;
  31.  
  32.         yMax = cam.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - offset;
  33.     }
  34.  
  35.     // Update is called once per frame
  36.     void Update(){
  37.         Move();
  38.         Fire();
  39.     }
  40.  
  41.     void Fire()
  42.     {
  43.         if (Input.GetKeyDown(KeyCode.Space)){
  44.             GameObject bul = Instantiate(Bullet, SpawnPoint.position, Quaternion.identity);
  45.             bul.GetComponent<Rigidbody2D>().velocity = new Vector2(0f, bulletspeed);
  46.         }
  47.     }
  48.  
  49.  
  50.     void Move(){
  51.         //Horizontal Axis
  52.         float deltaX = transform.position.x + Input.GetAxis("Horizontal") * Time.deltaTime * speed;
  53.         //Vertical Axis
  54.         float deltaY = transform.position.y + Input.GetAxis("Vertical") * Time.deltaTime * speed;
  55.  
  56.         deltaX = Mathf.Clamp(deltaX, xMin, xMax);
  57.         deltaY = Mathf.Clamp(deltaY, yMin, yMax);
  58.         /*if(deltaX <= xMin){
  59.             deltaX = xMin;
  60.         }
  61.         if(deltaX >= xMax){
  62.             deltaX = xMax;
  63.         }*/
  64.  
  65.         /*if(deltaY <= yMin){
  66.             deltaY = yMin;
  67.         }
  68.         if(deltaY >= yMax){
  69.             deltaY = yMax;
  70.         }*/
  71.         Vector3 delta = new Vector2(deltaX, deltaY);
  72.         transform.position = delta;
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement