Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=========== Движение персонажа ===========
- //Источник: https://www.youtube.com/watch?v=AXoH7Rm8B2U&t=101s
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- public float speed = 1f;
- // Start is called before the first frame update
- void Start()
- {
- }
- // Update is called once per frame
- void Update()
- {
- float movement = Input.GetAxis("Horizontal");
- transform.position += new Vector3(movement, 0, 0) * speed * Time.deltaTime;
- float vvv = Input.GetAxis("Vertical");
- transform.position += new Vector3(0, vvv, 0) * speed * Time.deltaTime;
- }
- }
- //=========== Комплексный скрипт ===========
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- public float speed = 1f; // Скорость
- public string data; // Простой текст
- public int money = 0; // Деньги игрока
- // Звук по туториалу https://www.youtube.com/watch?v=XlnFN6gBYEA
- public AudioSource testSound;
- // Start is called before the first frame update
- void Start()
- {
- testSound = GetComponent<AudioSource>(); // Звук
- }
- // Update is called once per frame
- void Update()
- {
- // Движение по горизонтали
- float movement = Input.GetAxis("Horizontal");
- transform.position += new Vector3(movement, 0, 0) * speed * Time.deltaTime;
- // Движение по вертикали
- float vvv = Input.GetAxis("Vertical");
- transform.position += new Vector3(0, vvv, 0) * speed * Time.deltaTime;
- // Поворот влево-вправо
- if(Input.GetAxis("Horizontal") < 0){
- GetComponent<SpriteRenderer>().flipX = true;
- }
- if(Input.GetAxis("Horizontal") > 0){
- GetComponent<SpriteRenderer>().flipX = false;
- }
- // Поворот вверх-вниз
- if(Input.GetAxis("Vertical") < 0){
- GetComponent<SpriteRenderer>().flipY = true;
- }
- if(Input.GetAxis("Vertical") > 0){
- GetComponent<SpriteRenderer>().flipY = false;
- }
- }
- void OnTriggerEnter2D(Collider2D collider)
- {
- data = "Касание";
- Debug.Log(data);
- // Проигрывание звука
- testSound.Play();
- /*
- Деньги простые
- money = 1;
- Debug.Log(money);
- */
- money = money+1; // Прибавляем 1 к балансу денег
- Debug.Log(money);
- speed = 4f; // Увеличиваем скорость до 4
- }
- }
- //=========== Генерация объектов ===========
- //По уроку https://www.youtube.com/watch?v=2CeedUmODOI
- //Перед запуском скрипта в инспекторе надо объекты из префабов в массив вручную перетащить на видео это с 4:15
- public class CircleCode : MonoBehaviour
- {
- public GameObject[] objects;
- void Start()
- {
- Instantiate(objects[0], new Vector3(0,0,0), Quaternion.identity);
- }
- void Update()
- {
- }
- }
- //=========== Проверка объекта ===========
- /*
- Всё работает если выбрать Circle на сцене и перетащить в Object в поле скрипта объект Diam
- Картинка https://disk.yandex.ru/i/2XzZc6ZN6EEvlA
- */
- public class CircleCode : MonoBehaviour
- {
- public GameObject Diam;
- void Start() {
- if (Diam == null){
- Debug.Log("Diam == null");
- } else {
- Debug.Log("Diam is not null");
- }
- }
- }
- //=========== Получение координат мыши относительно камеры (важно) ===========
- public class CircleCode : MonoBehaviour
- {
- public Vector3 mousePos;
- void Start() {
- }
- void Update() {
- mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- Debug.Log(mousePos);
- }
- }
- //=========== Автоматическое движение объекта вправо вариант 1 ===========
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CapsuleScript : MonoBehaviour
- {
- void Start(){}
- void Update()
- {
- transform.Translate(new Vector2(1, 0) * Time.deltaTime);
- }
- }
- //=========== Автоматическое движение объекта вправо вариант 2 ===========
- // Это более правильный вариант с Fixed временем обновления
- public class CapsuleScript : MonoBehaviour
- {
- //public Vector2 direction = new Vector2(1, 0);
- public float speed = 1f;
- void Start(){}
- void FixedUpdate()
- {
- transform.Translate(new Vector2(1, 0).normalized * speed);
- }
- }
- //=========== Отслеживание кликов мышки. Помещается строго в Update ===========
- void Update()
- {
- if (Input.GetMouseButtonDown(0))
- Debug.Log("Pressed primary button.");
- if (Input.GetMouseButtonDown(1))
- Debug.Log("Pressed secondary button.");
- if (Input.GetMouseButtonDown(2))
- Debug.Log("Pressed middle click.");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement