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 PlayerMovement : MonoBehaviour{
- [SerializeField] float speed = 8f;
- float bulletspeed = 10f;
- float xMin = 0f;
- float xMax = 0f;
- float yMin = 0f;
- float yMax = 0f;
- [SerializeField] Transform SpawnPoint;
- [SerializeField] GameObject Bullet;
- [SerializeField] float offset = 0.5f;
- // Start is called before the first frame update
- void Start()
- {
- CameraPoints();
- }
- private void CameraPoints()
- {
- Camera cam = Camera.main;
- xMin = cam.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + offset;
- xMax = cam.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - offset;
- yMin = cam.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + offset;
- yMax = cam.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - offset;
- }
- // Update is called once per frame
- void Update(){
- Move();
- Fire();
- }
- void Fire()
- {
- if (Input.GetKeyDown(KeyCode.Space)){
- GameObject bul = Instantiate(Bullet, SpawnPoint.position, Quaternion.identity);
- bul.GetComponent<Rigidbody2D>().velocity = new Vector2(0f, bulletspeed);
- }
- }
- void Move(){
- //Horizontal Axis
- float deltaX = transform.position.x + Input.GetAxis("Horizontal") * Time.deltaTime * speed;
- //Vertical Axis
- float deltaY = transform.position.y + Input.GetAxis("Vertical") * Time.deltaTime * speed;
- deltaX = Mathf.Clamp(deltaX, xMin, xMax);
- deltaY = Mathf.Clamp(deltaY, yMin, yMax);
- /*if(deltaX <= xMin){
- deltaX = xMin;
- }
- if(deltaX >= xMax){
- deltaX = xMax;
- }*/
- /*if(deltaY <= yMin){
- deltaY = yMin;
- }
- if(deltaY >= yMax){
- deltaY = yMax;
- }*/
- Vector3 delta = new Vector2(deltaX, deltaY);
- transform.position = delta;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement