- Katılım
- 10 Nisan 2020
- Mesajlar
- 1.923
- Makaleler
- 2
- Çözümler
- 7
Merhaba bir Udemy'den bakarak bir oyun yapmaya çalışıyorum. Oyunun kendi movement sistemi çalışıyor. Bir yere kadar da oynanıyor oyun. Bende dedim bari telefona çıktısını alıp bir de telefondan deneyeyim. Joystick ekledim sorunsuz çalışıyor fakat ateş etme ve dash atma butonlarını bir türlü ekleyemedim. ChatGPT'den yardım almayı denedim o da olmadı. Playercontrollerin içine ChatGPT'den aldığım kodlarla beraber deniyorum, Player'a butonu atıyorum, butonun on clickine scripti atıyorum fakat function bölümü boş oluyor gözükmüyor. Denediğim o zıplama ve ateş için olan kodları sildim şu anda movement ve Joystick'i çalışan kodları bıraktım. Ne yapmalıyım o 2 butonu yapıp mobilde oynayabilmek için?
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour.
{
public static PlayerController instance;
public float moveSpeed;
private Vector2 moveInput;
public Rigidbody2D theRB;
public Transform gunArm;
//private Camera theCam;
public Animator anim;
/* public GameObject bulletToFire;
public Transform firePoint;
public float timeBetweenShots;
private float shotCounter; */
public SpriteRenderer bodySR;
private float activeMoveSpeed;
public float dashSpeed = 8f, dashLength = .5f, dashCooldown = 1f, dashInvincibility = .5f;
[HideInInspector]
public float dashCounter;
private float dashCoolCounter;
[HideInInspector]
public bool canMove = true;
public List<Gun> availableGuns = new List<Gun>();
[HideInInspector]
public int currentGun;
// Joystick.
[SerializeField] private FixedJoystick fixedJoystick;
private void Awake()
{
instance = this;
DontDestroyOnLoad(gameObject);
}
// Start is called before the first frame update.
void Start()
{
activeMoveSpeed = moveSpeed;
UIController.instance.currentGun.sprite = availableGuns[currentGun].gunUI;
UIController.instance.gunText.text = availableGuns[currentGun].weaponName;
}
// Update is called once per frame.
void Update()
{
if (canMove && !LevelManager.instance.isPaused)
{
// Klavye girişlerini al.
moveInput.x = Input.GetAxisRaw("Horizontal");
moveInput.y = Input.GetAxisRaw("Vertical");
// Joystick girişlerini ekle.
moveInput.x += fixedJoystick.Horizontal;
moveInput.y += fixedJoystick.Vertical;
moveInput.Normalize();
theRB.velocity = moveInput * activeMoveSpeed;
Vector3 mousePos = Input.mousePosition;
Vector3 screenPoint = CameraController.instance.mainCamera.WorldToScreenPoint(transform.localPosition);
if (mousePos.x < screenPoint.x)
{
transform.localScale = new Vector3(-1f, 1f, 1f);
gunArm.localScale = new Vector3(-1f, -1f, 1f);
}
else.
{
transform.localScale = Vector3.one;
gunArm.localScale = Vector3.one;
}
Vector2 offset = new Vector2(mousePos.x - screenPoint.x, mousePos.y - screenPoint.y);
float angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
gunArm.rotation = Quaternion.Euler(0, 0, angle);
if (Input.GetKeyDown(KeyCode.Tab))
{
if (availableGuns.Count > 0)
{
currentGun++;
if (currentGun >= availableGuns.Count)
{
currentGun = 0;
}
SwitchGun();
}
else.
{
Debug.LogError("Player has no guns!");
}
}
if (Input.GetKeyDown(KeyCode.Space))
{
if (dashCoolCounter <= 0 && dashCounter <= 0)
{
activeMoveSpeed = dashSpeed;
dashCounter = dashLength;
anim.SetTrigger("dash");
PlayerHealthController.instance.MakeInvincible(dashInvincibility);
AudioManager.instance.PlaySFX(8);
}
}
if (dashCounter > 0)
{
dashCounter -= Time.deltaTime;
if (dashCounter <= 0)
{
activeMoveSpeed = moveSpeed;
dashCoolCounter = dashCooldown;
}
}
if (dashCoolCounter > 0)
{
dashCoolCounter -= Time.deltaTime;
}
if (moveInput != Vector2.zero)
{
anim.SetBool("isMoving", true);
}
else.
{
anim.SetBool("isMoving", false);
}
}
else.
{
theRB.velocity = Vector2.zero;
anim.SetBool("isMoving", false);
}
}
public void SwitchGun()
{
foreach (Gun theGun in availableGuns)
{
theGun.gameObject.SetActive(false);
}
availableGuns[currentGun].gameObject.SetActive(true);
UIController.instance.currentGun.sprite = availableGuns[currentGun].gunUI;
UIController.instance.gunText.text = availableGuns[currentGun].weaponName;
AudioManager.instance.PlaySFX(6);
}
}