Unity Koddaki hata nedir?

wewe12212

Hectopat
Katılım
4 Mayıs 2022
Mesajlar
323
Çözümler
1
Daha fazla  
Cinsiyet
Erkek
Merhaba arkadaşlar. Sandviç yapma oyunu yapıyorum. Sandviç hazır olunca elimize hazır prefab sandviç geliyor ama tezgahtaki kalan hazırladığım objeler silinmiyor duruyor. Yardımcı olabilecek biri var mı? Teşekkürler.
Kod:
Public class sandvichmaker: Monobehaviour.
{
 Private enum sandwichstate { empty, bread, cheese, tomato, bun, Complete }
 Private sandwichstate currentstate = sandwichstate. Empty;

 Public gameobject breadprefab;
 Public gameobject cheeseprefab;
 Public gameobject tomatoprefab;
 Public gameobject completesandwichprefab;
 Public transform handposition;
 Public transform tableposition;
 Public gameobject Crosshair;
 Public float distance = 5F;

 Public bool isunlimitedpickupbread = true;
 Public bool isunlimitedpickupothers = false;
 Public bool isunlimitedpickupall = false;

 Private gameobject currentıtem;
 Private bool issandwichplaced = false;
 Private Vector3 lastplacedposition = Vector3.Zero;

 Void Start()
 {
 crosshair.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
 }

 Void Update()
 {
 İf (ınput. Getmousebuttondown(0))
 {
 Raycasthit hit;
 Ray ray = camera. Main. Screenpointtoray(ınput. Mouseposition);

 İf (physics. Raycast(ray, out hit, distance))
 {
 İf (hit. Transform.comparetag("bread") && currentstate == sandwichstate. Empty)
 {
 İf (isunlimitedpickupbread || isunlimitedpickupall)
 {
 Pickupıtem(hit. Transform. Gameobject, breadprefab);
 Currentstate = sandwichstate. Bread;
 }
 }
 Else if (hit. Transform.comparetag("cheese") && currentstate == sandwichstate. Bread)
 {
 İf (isunlimitedpickupothers || isunlimitedpickupall)
 {
 Pickupıtem(hit. Transform. Gameobject, cheeseprefab);
 Currentstate = sandwichstate. Cheese;
 }
 }
 Else if (hit. Transform.comparetag("tomato") && currentstate == sandwichstate. Cheese)
 {
 İf (isunlimitedpickupothers || isunlimitedpickupall)
 {
 Pickupıtem(hit. Transform. Gameobject, tomatoprefab);
 Currentstate = sandwichstate. Tomato;
 }
 }
 Else if (hit. Transform.comparetag("bun") && currentstate == sandwichstate. Tomato)
 {
 İf (isunlimitedpickupbread || isunlimitedpickupall)
 {
 Pickupıtem(hit. Transform. Gameobject, breadprefab);
 Currentstate = sandwichstate. Bun;
 }
 }
 }
 }

 İf (ınput. Getkeydown(keycode. E) && currentıtem!= null)
 {
 İf (IsLookingAtTable())
 {
 PlaceItemOnTable();
 İf (currentstate == sandwichstate. Bun && issandwichplaced)
 {
 CompleteSandwich();
 }
 }
 }
 }

 Private Void pickupıtem(gameobject originalıtem, gameobject itemprefab)
 {
 İf (currentıtem!= null) return;
 Currentıtem = ınstantiate(itemprefab, handposition. Position, quaternion. İdentity);
 Currentıtem. Transform. Setparent(handposition);

 İf (!(isunlimitedpickupall || (itemprefab.comparetag("bread") && isunlimitedpickupbread) ||
 (İtemprefab.comparetag("cheese") && isunlimitedpickupothers) ||
 (İtemprefab.comparetag("tomato") && isunlimitedpickupothers) ||
 (İtemprefab.comparetag("bun") && isunlimitedpickupbread)))
 {
 Destroy(originalıtem);
 }
 }

 Private Void PlaceItemOnTable()
 {
 İf (currentıtem!= null)
 {
 Currentıtem. Transform. Setparent(null);
 Vector3 placementposition = tableposition. Position + Vector3.up * 0.1F;

 İf (lastplacedposition!= Vector3.Zero)
 {
 Placementposition = lastplacedposition + New Vector3(0, 0.1F, 0);
 }

 Currentıtem. Transform. Position = placementposition;
 Lastplacedposition = currentıtem. Transform. Position;

 Currentıtem = null;
 İssandwichplaced = true;
 }
 }

 Private Void CompleteSandwich()
 {
 İf (currentıtem!= null) destroy(currentıtem);
 Gameobject completesandwich = ınstantiate(completesandwichprefab, handposition. Position, quaternion. İdentity);
 Completesandwich. Transform. Setparent(handposition);

 DestroyAllItemsOnTable();
 }

 Private Void DestroyAllItemsOnTable()
 {
 Foreach (transform item in tableposition)
 {
 Destroy(item. Gameobject);
 }
 }

 Private bool IsLookingAtTable()
 {
 Raycasthit hit;
 Ray ray = camera. Main. Screenpointtoray(ınput. Mouseposition);

 İf (physics. Raycast(ray, out hit, distance))
 {
 İf (hit. Transform.comparetag("table"))
 {
 Return true;
 }
 }

 Return false;
 }
}
 
Son düzenleyen: Moderatör:
ChatGPT ile düzeltirdim birkaç hata olduğunu söyleyip kodu düzeltiğini söyledi ne kadar doğrudur bilmiyorum ama böyle bir dene istersen.

using UnityEngine;

public class SandwichMaker : MonoBehaviour
{
private enum SandwichState { Empty, Bread, Cheese, Tomato, Bun, Complete }
private SandwichState currentState = SandwichState.Empty;

public GameObject breadPrefab;
public GameObject cheesePrefab;
public GameObject tomatoPrefab;
public GameObject completeSandwichPrefab;
public Transform handPosition;
public Transform tablePosition;
public GameObject crosshair;
public float distance = 5f;

public bool isUnlimitedPickupBread = true;
public bool isUnlimitedPickupOthers = false;
public bool isUnlimitedPickupAll = false;

private GameObject currentItem;
private bool isSandwichPlaced = false;
private Vector3 lastPlacedPosition = Vector3.zero;

void Start()
{
crosshair.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
}

void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

if (Physics.Raycast(ray, out hit, distance))
{
if (hit.transform.CompareTag("bread") && currentState == SandwichState.Empty)
{
if (isUnlimitedPickupBread || isUnlimitedPickupAll)
{
PickUpItem(hit.transform.gameObject, breadPrefab);
currentState = SandwichState.Bread;
}
}
else if (hit.transform.CompareTag("cheese") && currentState == SandwichState.Bread)
{
if (isUnlimitedPickupOthers || isUnlimitedPickupAll)
{
PickUpItem(hit.transform.gameObject, cheesePrefab);
currentState = SandwichState.Cheese;
}
}
else if (hit.transform.CompareTag("tomato") && currentState == SandwichState.Cheese)
{
if (isUnlimitedPickupOthers || isUnlimitedPickupAll)
{
PickUpItem(hit.transform.gameObject, tomatoPrefab);
currentState = SandwichState.Tomato;
}
}
else if (hit.transform.CompareTag("bun") && currentState == SandwichState.Tomato)
{
if (isUnlimitedPickupBread || isUnlimitedPickupAll)
{
PickUpItem(hit.transform.gameObject, breadPrefab);
currentState = SandwichState.Bun;
}
}
}
}

if (Input.GetKeyDown(KeyCode.E) && currentItem != null)
{
if (IsLookingAtTable())
{
PlaceItemOnTable();
if (currentState == SandwichState.Bun && isSandwichPlaced)
{
CompleteSandwich();
}
}
}
}

private void PickUpItem(GameObject originalItem, GameObject itemPrefab)
{
if (currentItem != null) return;

currentItem = Instantiate(itemPrefab, handPosition.position, Quaternion.identity);
currentItem.transform.SetParent(handPosition);

if (!(isUnlimitedPickupAll || (itemPrefab.CompareTag("bread") && isUnlimitedPickupBread) ||
(itemPrefab.CompareTag("cheese") && isUnlimitedPickupOthers) ||
(itemPrefab.CompareTag("tomato") && isUnlimitedPickupOthers) ||
(itemPrefab.CompareTag("bun") && isUnlimitedPickupBread)))
{
Destroy(originalItem);
}
}

private void PlaceItemOnTable()
{
if (currentItem != null)
{
currentItem.transform.SetParent(null);
Vector3 placementPosition = tablePosition.position + Vector3.up * 0.1f;

if (lastPlacedPosition != Vector3.zero)
{
placementPosition = lastPlacedPosition + new Vector3(0, 0.1f, 0);
}

currentItem.transform.position = placementPosition;
lastPlacedPosition = currentItem.transform.position;

currentItem = null;
isSandwichPlaced = true;
}
}

private void CompleteSandwich()
{
if (currentItem != null) Destroy(currentItem);
GameObject completeSandwich = Instantiate(completeSandwichPrefab, handPosition.position, Quaternion.identity);
completeSandwich.transform.SetParent(handPosition);

DestroyAllItemsOnTable();
}

private void DestroyAllItemsOnTable()
{
foreach (Transform item in tablePosition)
{
Destroy(item.gameObject);
}
}

private bool IsLookingAtTable()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

if (Physics.Raycast(ray, out hit, distance))
{
if (hit.transform.CompareTag("table"))
{
return true;
}
}

return false;
}
}
 

Technopat Haberler

Yeni konular

Geri
Yukarı