Zerstören gameObject, um eine andere zu benutzenC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Zerstören gameObject, um eine andere zu benutzen

Post by Anonymous »

Also mache ich ein Spiel in der Einheit, in dem Sie Papier als Hauptmechaniker in einen Drucker ablegen, aber sobald das erste Papier abgelagert ist, da dies der variable Set im "Papier" ist, funktioniert der Rest der Teile nicht und wird nicht abgelagert. unten: < /p>

Code: Select all

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
using TMPro;

public class DepositPaper : MonoBehaviour
{
public GameObject Paper;
public int PaperCount = 0;

public TextMeshPro paperText;

public float raycastDistance = 100f;
public LayerMask interactableLayer;

// Start is called once before the first execution of Update after the MonoBehaviour is created

void Start()
{

}

void destroyPaper()
{
Destroy(Paper);
}

// Update is called once per frame
void Update()
{
Ray ray = new Ray(transform.position, transform.forward);

if (Physics.Raycast(ray, out RaycastHit hit, raycastDistance, interactableLayer) && hit.collider.gameObject.CompareTag("Printer"))
{
if(Input.GetKeyDown("e") )
{
destroyPaper();
PaperCount++;
paperText.text = "Paper:" + PaperCount.ToString() + "/11";
}
}

if(PaperCount == 2)
{
GameObject[] deadTaggedObjects = GameObject.FindGameObjectsWithTag("Dead_1");
foreach (GameObject deadtagged in deadTaggedObjects)
{
deadtagged.SetActive(false);
}
}
}
}
```

Scipt that declares if object can be picked up:

using UnityEngine;
/// 
/// Attach this class to make object pickable.
/// 
[RequireComponent(typeof(Rigidbody))]
public class PickableItem : MonoBehaviour
{
// Reference to the rigidbody
private Rigidbody rb;
public Rigidbody Rb =>  rb;
/// 
/// Method called on initialization.
/// 
private void Awake()
{
// Get reference to the rigidbody
rb = GetComponent();
}
}

Script that picks up the object:

using UnityEngine;

/// 
/// Simple example of Grabbing system.
/// 
public class SimpleGrabSystem : MonoBehaviour
{
// Reference to the character camera.
[SerializeField]
private Camera characterCamera;

// Reference to the slot for holding picked item.
[SerializeField]
private Transform slot;

// Reference to the currently held item.
public PickableItem pickedItem;

/// 
/// Method called very frame.
/// 
private void Update()
{
// Execute logic only on button pressed
if (Input.GetKeyDown("f"))
{
// Check if player picked some item already
if (pickedItem)
{
// If yes, drop picked item
DropItem(pickedItem);
}
else
{
// If no, try to pick item in front of the player
// Create ray from center of the screen
var ray = characterCamera.ViewportPointToRay(Vector3.one * 0.5f);
RaycastHit hit;
// Shot ray to find object to pick
if (Physics.Raycast(ray, out hit, 1.5f))
{
// Check if object is pickable
var pickable = hit.transform.GetComponent();

// If object has PickableItem class
if (pickable)
{
// Pick it
PickItem(pickable);
}
}
}
}
}

/// 
/// Method for picking up item.
/// 
/// Item.
private void PickItem(PickableItem item)
{
// Assign reference
pickedItem = item;

// Disable rigidbody and reset velocities
item.Rb.isKinematic = true;
item.Rb.linearVelocity = Vector3.zero;
item.Rb.angularVelocity = Vector3.zero;

// Set Slot as a parent
item.transform.SetParent(slot);

// Reset position and rotation
item.transform.localPosition = Vector3.zero;
item.transform.localEulerAngles = Vector3.zero;

}

/// 
/// Method for dropping item.
/// 
/// Item.
private void DropItem(PickableItem item)
{
// Remove reference
pickedItem = null;

// Remove parent
item.transform.SetParent(null);

// Enable rigidbody
item.Rb.isKinematic = false;

// Add force to throw item a little bit

item.Rb.AddForce(item.transform.forward * 2,
ForceMode.VelocityChange);
}
}

![Script attached to Camera](https://i.sstatic.net/H3ic5QjO.png)

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post