Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerScript : MonoBehaviour {
  5.  
  6.     public float playerVelocity;
  7.     private Vector3 playerPosition;
  8.  
  9.     // используйте этот метод для инициализации
  10.     void Start () {
  11.         // получим начальную позицию платформы
  12.         playerPosition = gameObject.transform.position;
  13.     }
  14.      
  15.     // Update вызывается при отрисовке каждого кадра игры
  16.     void Update () {
  17.         // горизонтальное движение
  18.         playerPosition.x += Input.GetAxis ("Horizontal") * playerVelocity;
  19.  
  20.         // выход из игры
  21.         if (Input.GetKeyDown(KeyCode.Escape)){
  22.             Application.Quit();
  23.         }
  24.  
  25.         // обновим позицию платформы
  26.         transform.position = playerPosition;
  27.        
  28.         // проверка выхода за границы
  29.         if (playerPosition.x < -boundary) {
  30.             transform.position = new Vector3 (-boundary, playerPosition.y, playerPosition.z);
  31.         }
  32.         if (playerPosition.x > boundary) {
  33.             transform.position = new Vector3(boundary, playerPosition.y, playerPosition.z);    
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement