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: