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: