Zmiana sterowania strzałkami na touchpad

Ogólne pytania na temat Unity3D

Zmiana sterowania strzałkami na touchpad

Postprzez DenDonnar » 21 Maj 2020, 19:17

Hej
Już mam projekt zakończonej gry na PC. Jest to zwykły platformer, którym się steruję za pomocą strzałek, ale chciał bym dodac możliwość grania w tą gre z mojego Xiaomi Redmi Note 8 Pro :mrgreen: . Pomysł aby rozdzielić ekran na 2 cięści (wertykalnie) i po wciśnięciu prawej strony moja figura idzie w prawo i to samo w lewo. Tu poniżej script ;)

Kod: Zaznacz wszystko
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
   //this is a master script for player character, look for coments below, I'll try to explain what each method does
   //if you will stuck with anything please watch tutorial videos, or just send me an email pamargames@gmail.com


   //this is VFX that spawns everytime player respawns (after death, or when entering new level)
   public GameObject PlayerStartFx;

   //those 2 VFX are attached to the player to constantly spawn particles, its a small detail.
   public GameObject playerConstantFX;
   public GameObject playerConstantFX2;

   //this is object in the scene which determines where player will spawn
   GameObject playerStartObject;

   //this is to check if player is present in the scene
   bool playerLocation;

   //this is simple VFX, it spawns everytime when player dies
   public GameObject deathFX;


   void Start ()
   {
      playerLocation = true;
      playerStartObject = GameObject.FindGameObjectWithTag("playerStart");
   }

   void FixedUpdate ()
   {
      if(playerLocation == true)
      {
         //if player go blow screen is will count as death and player will spawn again, you can change value -6
         if(gameObject.transform.position.y <= -6)
         {
            gameObject.GetComponent<TrailRenderer>().enabled = false;
            Invoke("playerStartFunction",1f);
            playerLocation = false;
         }
      }
   }

   //this is function which disables all VFX attached to player upon death
   void playerTouchedenemy()
   {
      gameObject.GetComponent<TrailRenderer>().enabled = false;
      Invoke("playerStartFunction",1f);
      playerLocation = false;
   }

   //this enables all VFX when player being spawn, usally after death
   void playerStartFunction()
   {
      gameObject.GetComponent<TrailRenderer>().enabled = true;
      gameObject.transform.position = playerStartObject.transform.position;
      Instantiate(PlayerStartFx,playerStartObject.transform.position,Quaternion.identity);
      playerLocation = true;
   }

   //colliders
   void OnTriggerEnter(Collider other)
   {
      //this is being executed when player collide with "portal" tag which is entrance to next level
      if(other.gameObject.tag == "portal")
      {
         gameObject.GetComponent<Collider>().enabled = false;
         gameObject.GetComponent<Renderer>().enabled = false;
         gameObject.GetComponent<Rigidbody>().isKinematic = true;
         gameObject.GetComponent<Rigidbody>().useGravity = false;
         gameObject.GetComponent<TrailRenderer>().time = 0;
         playerConstantFX.GetComponent<ParticleSystem>().emissionRate = 0;
         playerConstantFX2.SetActive(false);
      }

      //this is executed when player collide will enemy with "spiky" tag
      if(other.gameObject.tag == "spiky")
      {
         Instantiate(deathFX,gameObject.transform.position,Quaternion.identity);
         gameObject.GetComponent<TrailRenderer>().enabled = false;
         gameObject.transform.position = new Vector3 (0,100,0);
         Invoke("playerStartFunction",1f);
      }
   }

   //this function is responsible for player bounce off the floor, you can change 2000 value to whatever you want
   void OnCollisionEnter(Collision col)
   {
      if(col.gameObject.tag == "Floor")
      {
            GetComponent<Rigidbody>().AddForce(0,2000 * Time.deltaTime,0);
      }

   }

   //these are simple controls which SHOULD change if you developing for mobile of you want to add PAD controls
   void Update()
   {
      if (Input.GetKey(KeyCode.LeftArrow))
      {
         GetComponent<Rigidbody>().AddForce(-50f * Time.deltaTime,0,0);
      }
      if (Input.GetKey(KeyCode.RightArrow))
      {
         GetComponent<Rigidbody>().AddForce(new Vector3(50f * Time.deltaTime,0,0));
      }
   }

}

DenDonnar
 
Posty: 1
Rejestracja: 21 Maj 2020, 19:05
Has thanked: 0 time
Been thanked: 0 time

Re: Zmiana sterowania strzałkami na touchpad

Postprzez Ether_141 » 21 Maj 2020, 20:54

Ten skrypt trochę brzydko napisany. Cacheuj te komponenty Rigidbody, TrailRenderer itd. a nie wołaj GetComponent za każdym razem. Co do sterowania to bym zrobił przezroczyte dwa obrazki na UI i w momencie kliknięcia (OnClick event) jednego czy drugiego poruszał postacią.
Awatar użytkownika
Ether_141
 
Posty: 499
Rejestracja: 29 Sie 2015, 11:47
Has thanked: 0 time
Been thanked: 0 time


Wróć do Wsparcie Unity

Kto jest na forum

Użytkownicy przeglądający to forum: Brak zarejestrowanych użytkowników oraz 2 gości

cron