C# Unity Nesneyi alma ve atma kodu kodu iyi mi?

SORASIM GELDİ

Centipat
Katılım
17 Ocak 2024
Mesajlar
35
Daha fazla  
Cinsiyet
Erkek
Unity'de nesneyi alma ve atma kodu yazdım. Kod performanslımı, iyi mi değilse nasıl bir kod yazabilirim?


Kod:
Pickup Script:



using Unity.Mathematics;

using UnityEngine;

using UnityEngine.UI;



public class Pickup : MonoBehaviour

{

    public float kuvvet = 10.0f;

    public float maxDistance = 5.0f;

    public LayerMask rayLayerMask;

    public Transform _camera;

    public Transform hand;

    public Image isaret;

    public string isaretUIColor = "#81FF85";



    private bool lookingAtTheObject = false;

    private bool objectReceived = false;

    private Transform lookingAtTheObjectTransform;

    private Vector3 lookingAtTheObjectLocalscale;

    private Rigidbody lookingAtTheObjectRigidbody;

    private Collider lookingAtTheObjectCollider;

    UnityEngine.Color newColor;



    private void FixedUpdate()

    {

        RaycastHit hit;

        if (RayManager.RaycastFromCamera(out hit, maxDistance, rayLayerMask))

        {

            if (hit.collider.gameObject.tag == "Pickup" && !lookingAtTheObject && !objectReceived)

            {



                lookingAtTheObject = true;



                if (UnityEngine.ColorUtility.TryParseHtmlString(isaretUIColor, out newColor))

                {

                    isaret.color = newColor;

                }



                lookingAtTheObjectTransform = hit.collider.transform;

                lookingAtTheObjectLocalscale = hit.transform.localScale;

                lookingAtTheObjectRigidbody = hit.collider.attachedRigidbody;

                lookingAtTheObjectCollider = hit.collider;

            }

        }

        else

        {

            if (lookingAtTheObject && !objectReceived)

            {

                lookingAtTheObject = false;

            }

            if (!objectReceived)

            {

                isaret.color = UnityEngine.Color.white;

            }

        }

    }

    void Update()

    {

        if (!lookingAtTheObject && !objectReceived && (lookingAtTheObjectCollider != null || lookingAtTheObjectTransform != null || lookingAtTheObjectLocalscale != Vector3.zero || lookingAtTheObjectRigidbody != null))

        {

            ResetObjectInfo();

        }



        if (lookingAtTheObject)

        {

            if (Input.GetKeyDown(KeyCode.E) && !objectReceived)

            {

                GetTheObject();

                lookingAtTheObjectTransform.position = Vector3.Lerp(lookingAtTheObjectTransform.position, hand.position, Time.deltaTime * 5.0f);

                lookingAtTheObjectTransform.rotation = Quaternion.Lerp(lookingAtTheObjectTransform.rotation, hand.rotation, Time.deltaTime * 5.0f);

                lookingAtTheObjectTransform.localScale = Vector3.Lerp(lookingAtTheObjectTransform.localScale, lookingAtTheObjectLocalscale, Time.deltaTime * 5.0f);

            }

        }



        if (objectReceived)

        {

            isaret.color = UnityEngine.Color.white;

            if (Input.GetKeyDown(KeyCode.Q) && objectReceived)

            {

                DropObject();

            }

        }



        if (Input.GetKeyDown(KeyCode.N))

        {

            Debug.Log($"Collider= {lookingAtTheObjectCollider}, LocalScale= {lookingAtTheObjectLocalscale}, Rigidbody= {lookingAtTheObjectRigidbody}, Transform= {lookingAtTheObjectTransform}, Enabled= {lookingAtTheObject}");

        }

    }



    void ResetObjectInfo()

    {

        if (lookingAtTheObjectCollider != null)

        {

            lookingAtTheObjectCollider = null;

        }

        if (lookingAtTheObjectLocalscale != Vector3.zero)

        {

            lookingAtTheObjectLocalscale = Vector3.zero;

        }

        if (lookingAtTheObjectRigidbody != null)

        {

            lookingAtTheObjectRigidbody = null;

        }

        if (lookingAtTheObjectTransform != null)

        {

            lookingAtTheObjectTransform = null;

        }

    }



    public void GetTheObject()

    {

        objectReceived = true;

        lookingAtTheObjectRigidbody.isKinematic = true;

        lookingAtTheObjectCollider.isTrigger = true;

        lookingAtTheObjectTransform.SetParent(hand);

        lookingAtTheObjectTransform.position = hand.position;

        lookingAtTheObjectTransform.rotation = hand.rotation;

        lookingAtTheObjectTransform.localScale = lookingAtTheObjectLocalscale;

    }



    public void DropObject()

    {

        objectReceived = false;

        lookingAtTheObjectRigidbody.isKinematic = false;

        lookingAtTheObjectCollider.isTrigger = false;

        lookingAtTheObjectTransform.SetParent(null);

        lookingAtTheObjectTransform.localScale = lookingAtTheObjectLocalscale;

        Vector3 pos = _camera.forward;

        lookingAtTheObjectRigidbody.AddForce(pos * kuvvet, ForceMode.Impulse);

        ResetObjectInfo();

    }

}
 
Son düzenleme:
Nesne al bırak kodu sanırım ben de lisedeyken sarmıştım biraz bu şekilde kullanman daha doğru olur.

Kod:
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.UI;

public class Pickup : MonoBehaviour
{
    public float kuvvet = 10.0f;
    public float maxDistance = 5.0f;
    public LayerMask rayLayerMask;
    public Transform _camera;
    public Transform hand;
    public Image isaret;
    public string isaretUIColor = "#81FF85";

    private bool lookingAtTheObject = false;
    private bool objectReceived = false;
    private Transform lookingAtTheObjectTransform;
    private Vector3 lookingAtTheObjectLocalscale;
    private Rigidbody lookingAtTheObjectRigidbody;
    private Collider lookingAtTheObjectCollider;
    UnityEngine.Color newColor;

    private void FixedUpdate()
    {
        RaycastHit hit;
        if (RayManager.RaycastFromCamera(out hit, maxDistance, rayLayerMask))
        {
            if (hit.collider.gameObject.tag == "Pickup" && !lookingAtTheObject && !objectReceived)
            {
                SetLookingAtTheObject(hit);
            }
        }
        else
        {
            if (lookingAtTheObject && !objectReceived)
            {
                lookingAtTheObject = false;
            }
            if (!objectReceived)
            {
                isaret.color = UnityEngine.Color.white;
            }
        }
    }

    private void Update()
    {
        if (!lookingAtTheObject && !objectReceived && IsObjectInfoSet())
        {
            ResetObjectInfo();
        }

        if (lookingAtTheObject)
        {
            if (Input.GetKeyDown(KeyCode.E) && !objectReceived)
            {
                GetTheObject();
                MoveObjectTowardsHand();
            }
        }

        if (objectReceived)
        {
            isaret.color = UnityEngine.Color.white;
            if (Input.GetKeyDown(KeyCode.Q))
            {
                DropObject();
            }
        }

        if (Input.GetKeyDown(KeyCode.N))
        {
            Debug.Log($"Collider= {lookingAtTheObjectCollider}, LocalScale= {lookingAtTheObjectLocalscale}, Rigidbody= {lookingAtTheObjectRigidbody}, Transform= {lookingAtTheObjectTransform}, Enabled= {lookingAtTheObject}");
        }
    }

    private void SetLookingAtTheObject(RaycastHit hit)
    {
        lookingAtTheObject = true;

        if (UnityEngine.ColorUtility.TryParseHtmlString(isaretUIColor, out newColor))
        {
            isaret.color = newColor;
        }

        lookingAtTheObjectTransform = hit.collider.transform;
        lookingAtTheObjectLocalscale = hit.transform.localScale;
        lookingAtTheObjectRigidbody = hit.collider.attachedRigidbody;
        lookingAtTheObjectCollider = hit.collider;
    }

    private void ResetObjectInfo()
    {
        lookingAtTheObjectCollider = null;
        lookingAtTheObjectLocalscale = Vector3.zero;
        lookingAtTheObjectRigidbody = null;
        lookingAtTheObjectTransform = null;
    }

    private bool IsObjectInfoSet()
    {
        return lookingAtTheObjectCollider != null || lookingAtTheObjectTransform != null || lookingAtTheObjectLocalscale != Vector3.zero || lookingAtTheObjectRigidbody != null;
    }

    public void GetTheObject()
    {
        objectReceived = true;
        lookingAtTheObjectRigidbody.isKinematic = true;
        lookingAtTheObjectCollider.isTrigger = true;
        lookingAtTheObjectTransform.SetParent(hand);
        lookingAtTheObjectTransform.position = hand.position;
        lookingAtTheObjectTransform.rotation = hand.rotation;
        lookingAtTheObjectTransform.localScale = lookingAtTheObjectLocalscale;
    }

    public void DropObject()
    {
        objectReceived = false;
        lookingAtTheObjectRigidbody.isKinematic = false;
        lookingAtTheObjectCollider.isTrigger = false;
        lookingAtTheObjectTransform.SetParent(null);
        lookingAtTheObjectTransform.localScale = lookingAtTheObjectLocalscale;
        Vector3 pos = _camera.forward;
        lookingAtTheObjectRigidbody.AddForce(pos * kuvvet, ForceMode.Impulse);
        ResetObjectInfo();
    }

    private void MoveObjectTowardsHand()
    {
        lookingAtTheObjectTransform.position = Vector3.Lerp(lookingAtTheObjectTransform.position, hand.position, Time.deltaTime * 5.0f);
        lookingAtTheObjectTransform.rotation = Quaternion.Lerp(lookingAtTheObjectTransform.rotation, hand.rotation, Time.deltaTime * 5.0f);
        lookingAtTheObjectTransform.localScale = Vector3.Lerp(lookingAtTheObjectTransform.localScale, lookingAtTheObjectLocalscale, Time.deltaTime * 5.0f);
    }
}
Unity'nin kendi hazır kod paketleri de var bunlarla uğraşmana pek gerek yok.
 
Bir sorum daha var, aşağıda gönderdiğim resim gibi bir doku nasıl yapabilirim?

1734862010368.png
 

Technopat Haberler

Yeni konular

Geri
Yukarı