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
- {
- public int speed;
- public GameObject playerCamera;
- private string inputString;
- private int currentHorScreen;
- private int currentVertScreen;
- //Camera verticies are at +/- (8.9,5) at (0,0)
- private void Start()
- {
- currentHorScreen = 0;
- currentVertScreen = 0;
- }
- private void Update()
- {
- inputString = Input.inputString;
- 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
- {
- currentVertScreen += 1;
- playerCamera.GetComponent<Transform>().position += (Vector3.up * 10) ;
- }
- if (gameObject.transform.position.y < ((10 * currentVertScreen) - 5))
- {
- currentVertScreen -= 1;
- playerCamera.GetComponent<Transform>().position += (Vector3.down * 10);
- }
- if (gameObject.transform.position.x > ((17.8 * currentHorScreen) + 8.9))
- {
- currentHorScreen += 1;
- playerCamera.GetComponent<Transform>().position += (Vector3.right * 17.8f);
- }
- if (gameObject.transform.position.x < ((17.8 * currentHorScreen) - 8.9))
- {
- currentHorScreen -= 1;
- playerCamera.GetComponent<Transform>().position += (Vector3.left * 17.8f);
- }
- }
- void FixedUpdate()
- {
- float h = Input.GetAxis("Horizontal");
- float v = Input.GetAxis("Vertical");
- GetComponent<Rigidbody2D>().velocity = new Vector2(h, v) * speed; //getAxisRaw in [-1,1]
- }
- private void OnGUI()
- {
- GUILayout.BeginArea(new Rect(Screen.width / 2 - 200, -7, 400, 200));
- GUILayout.BeginHorizontal("box");
- GUILayout.Button(new GUIContent(((inputString == null) ? "" : inputString)));
- GUILayout.Button(new GUIContent(gameObject.transform.position.y.ToString()));
- GUILayout.Button(new GUIContent(gameObject.transform.position.x.ToString()));
- GUILayout.Button(new GUIContent(currentHorScreen.ToString()));
- GUILayout.Button(new GUIContent(currentVertScreen.ToString()));
- GUILayout.EndHorizontal();
- GUILayout.EndArea();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement