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>
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);
}
}

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] 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);
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); } } } } }
Wie zerstöre ich ein GameObject dauerhaft? Das heißt, ich möchte, dass das GameObject nur einmal vorhanden ist, wenn ich es in der aktuellen Szene zerstöre, und wenn ich dann dieselbe Szene neu lade,...
Ich habe ein System erstellt, das .FBX -Modelle dynamisch in den 3D -Raum importieren kann. Ich habe eine Klasse, die jedes Spielobjekt, das in die Szene importiert wurde, automatisch Mesh -Kollider...
Dies ist eine einfache Frage, daher bin ich überrascht, dass ich es nicht finden kann, um sie so zu fragen (entschuldige, wenn ich es verpasst habe), und es wird mir immer in den Sinn kommen, wenn...