Advertisement
zero50x

Unity

May 20th, 2022 (edited)
1,477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.45 KB | None | 0 0
  1. //=========== Движение персонажа ===========
  2. //Источник: https://www.youtube.com/watch?v=AXoH7Rm8B2U&t=101s
  3.  
  4.  
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8.  
  9. public class PlayerController : MonoBehaviour
  10. {
  11.     public float speed = 1f;
  12.     // Start is called before the first frame update
  13.     void Start()
  14.     {
  15.        
  16.     }
  17.  
  18.     // Update is called once per frame
  19.     void Update()
  20.     {
  21.         float movement = Input.GetAxis("Horizontal");
  22.         transform.position += new Vector3(movement, 0, 0) * speed * Time.deltaTime;
  23.  
  24.         float vvv = Input.GetAxis("Vertical");
  25.         transform.position += new Vector3(0, vvv, 0) * speed * Time.deltaTime;
  26.     }
  27. }
  28.  
  29.  
  30.  
  31. //=========== Комплексный скрипт ===========
  32.  
  33. using System.Collections;
  34. using System.Collections.Generic;
  35. using UnityEngine;
  36.  
  37. public class PlayerController : MonoBehaviour
  38. {
  39.     public float speed = 1f; // Скорость
  40.     public string data; // Простой текст
  41.     public int money = 0; // Деньги игрока
  42.  
  43.     // Звук по туториалу https://www.youtube.com/watch?v=XlnFN6gBYEA
  44.     public AudioSource testSound;
  45.  
  46.     // Start is called before the first frame update
  47.     void Start()
  48.     {
  49.         testSound = GetComponent<AudioSource>(); // Звук
  50.     }
  51.  
  52.     // Update is called once per frame
  53.     void Update()
  54.     {
  55.         // Движение по горизонтали
  56.         float movement = Input.GetAxis("Horizontal");
  57.         transform.position += new Vector3(movement, 0, 0) * speed * Time.deltaTime;
  58.        
  59.         // Движение по вертикали
  60.         float vvv = Input.GetAxis("Vertical");
  61.         transform.position += new Vector3(0, vvv, 0) * speed * Time.deltaTime;
  62.  
  63.         // Поворот влево-вправо
  64.         if(Input.GetAxis("Horizontal") < 0){
  65.             GetComponent<SpriteRenderer>().flipX = true;
  66.         }
  67.         if(Input.GetAxis("Horizontal") > 0){
  68.             GetComponent<SpriteRenderer>().flipX = false;
  69.         }
  70.  
  71.         // Поворот вверх-вниз
  72.         if(Input.GetAxis("Vertical") < 0){
  73.             GetComponent<SpriteRenderer>().flipY = true;
  74.         }
  75.         if(Input.GetAxis("Vertical") > 0){
  76.             GetComponent<SpriteRenderer>().flipY = false;
  77.         }
  78.     }
  79.  
  80.     void OnTriggerEnter2D(Collider2D collider)
  81.     {
  82.         data = "Касание";
  83.         Debug.Log(data);
  84.  
  85.         // Проигрывание звука
  86.         testSound.Play();
  87.  
  88.         /*
  89.         Деньги простые
  90.         money = 1;
  91.         Debug.Log(money);
  92.         */
  93.  
  94.         money = money+1; // Прибавляем 1 к балансу денег
  95.         Debug.Log(money);
  96.  
  97.  
  98.         speed = 4f; // Увеличиваем скорость до 4
  99.     }
  100.  
  101.  
  102. }
  103.  
  104.  
  105. //=========== Генерация объектов ===========
  106.  
  107. //По уроку https://www.youtube.com/watch?v=2CeedUmODOI
  108. //Перед запуском скрипта в инспекторе надо объекты из префабов в массив вручную перетащить на видео это с 4:15
  109. public class CircleCode : MonoBehaviour
  110. {
  111.     public GameObject[] objects;
  112.  
  113.     void Start()
  114.     {
  115.         Instantiate(objects[0], new Vector3(0,0,0), Quaternion.identity);
  116.     }
  117.  
  118.     void Update()
  119.     {
  120.  
  121.     }
  122. }
  123.  
  124.  
  125. //=========== Проверка объекта ===========
  126.  
  127. /*
  128. Всё работает если выбрать Circle на сцене и перетащить в Object в поле скрипта объект Diam
  129. Картинка https://disk.yandex.ru/i/2XzZc6ZN6EEvlA
  130. */
  131. public class CircleCode : MonoBehaviour
  132. {
  133.     public GameObject Diam;
  134.  
  135.     void Start() {
  136.         if (Diam == null){
  137.             Debug.Log("Diam == null");
  138.         } else {
  139.             Debug.Log("Diam is not null");
  140.         }
  141.     }
  142. }
  143.  
  144.  
  145.  
  146. //=========== Получение координат мыши относительно камеры (важно) ===========
  147.  
  148. public class CircleCode : MonoBehaviour
  149. {
  150.     public Vector3 mousePos;
  151.     void Start() {
  152.  
  153.     }
  154.     void Update() {
  155.         mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  156.         Debug.Log(mousePos);
  157.     }
  158. }
  159.  
  160.  
  161. //=========== Автоматическое движение объекта вправо вариант 1 ===========
  162.  
  163. using System.Collections;
  164. using System.Collections.Generic;
  165. using UnityEngine;
  166.  
  167. public class CapsuleScript : MonoBehaviour
  168. {
  169.     void Start(){}
  170.  
  171.     void Update()
  172.     {
  173.         transform.Translate(new Vector2(1, 0) * Time.deltaTime);
  174.     }
  175. }
  176.  
  177. //=========== Автоматическое движение объекта вправо вариант 2 ===========
  178. // Это более правильный вариант с Fixed временем обновления
  179.  
  180. public class CapsuleScript : MonoBehaviour
  181. {
  182.     //public Vector2 direction = new Vector2(1, 0);
  183.     public float speed = 1f;
  184.  
  185.     void Start(){}
  186.  
  187.     void FixedUpdate()
  188.     {
  189.         transform.Translate(new Vector2(1, 0).normalized * speed);
  190.     }
  191. }
  192.  
  193.  
  194.  
  195. //=========== Отслеживание кликов мышки. Помещается строго в Update ===========
  196.  
  197.     void Update()
  198.     {
  199.         if (Input.GetMouseButtonDown(0))
  200.             Debug.Log("Pressed primary button.");
  201.  
  202.         if (Input.GetMouseButtonDown(1))
  203.             Debug.Log("Pressed secondary button.");
  204.  
  205.         if (Input.GetMouseButtonDown(2))
  206.             Debug.Log("Pressed middle click.");
  207.            
  208.     }
  209.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement