Advertisement
Gremyo

Untitled

Jan 26th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7.     public int speed;
  8.     public GameObject playerCamera;
  9.     private string inputString;
  10.     private int currentHorScreen;
  11.     private int currentVertScreen;
  12.  
  13.     //Camera verticies are at +/- (8.9,5) at (0,0)
  14.  
  15.     private void Start()
  16.     {
  17.         currentHorScreen = 0;
  18.         currentVertScreen = 0;
  19.     }
  20.  
  21.     private void Update()
  22.     {
  23.         inputString = Input.inputString;
  24.         if (gameObject.transform.position.y > ((10 * currentVertScreen) + 5)) //These if statements check if the player is out of sight of the camera and will get the camera onto them
  25.         {
  26.             currentVertScreen += 1;
  27.             playerCamera.GetComponent<Transform>().position += (Vector3.up * 10) ;
  28.         }
  29.         if (gameObject.transform.position.y < ((10 * currentVertScreen) - 5))
  30.         {
  31.             currentVertScreen -= 1;
  32.             playerCamera.GetComponent<Transform>().position += (Vector3.down * 10);
  33.         }
  34.         if (gameObject.transform.position.x > ((17.8 * currentHorScreen) + 8.9))
  35.         {
  36.             currentHorScreen += 1;
  37.             playerCamera.GetComponent<Transform>().position += (Vector3.right * 17.8f);
  38.         }
  39.         if (gameObject.transform.position.x < ((17.8 * currentHorScreen) - 8.9))
  40.         {
  41.             currentHorScreen -= 1;
  42.             playerCamera.GetComponent<Transform>().position += (Vector3.left * 17.8f);
  43.         }
  44.     }
  45.  
  46.     void FixedUpdate()
  47.     {
  48.         float h = Input.GetAxis("Horizontal");
  49.         float v = Input.GetAxis("Vertical");
  50.         GetComponent<Rigidbody2D>().velocity = new Vector2(h, v) * speed; //getAxisRaw in [-1,1]
  51.     }
  52.  
  53.     private void OnGUI()
  54.     {
  55.         GUILayout.BeginArea(new Rect(Screen.width / 2 - 200, -7, 400, 200));
  56.         GUILayout.BeginHorizontal("box");
  57.         GUILayout.Button(new GUIContent(((inputString == null) ? "" : inputString)));
  58.         GUILayout.Button(new GUIContent(gameObject.transform.position.y.ToString()));
  59.         GUILayout.Button(new GUIContent(gameObject.transform.position.x.ToString()));
  60.         GUILayout.Button(new GUIContent(currentHorScreen.ToString()));
  61.         GUILayout.Button(new GUIContent(currentVertScreen.ToString()));
  62.         GUILayout.EndHorizontal();
  63.         GUILayout.EndArea();
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement