Witam mam problem z pewną rzeczą. Udało mi się stworzyć skrypt dzięki , któremu mogę podnosić obiekty w grze (tak jak np. w grze my summer car) ale chciałbym , żeby nie podnosiły się wszystkie obiekty w moim zasięgu tylko podnosił się ten , na który jest skierowany celownik . Na dole załączam skrypt.
Z góry dziękuje :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickUP : MonoBehaviour
{
public Transform player;
public Transform cam;
public float throwForce = 100f;
bool hasPlayer = false;
bool isHold = false;
public float iCan = 2f;
bool toch = false;
void Update()
{
float dist = Vector3.Distance(gameObject.transform.position,player.position);
if(dist <= 2f)
{
hasPlayer = true;
}
else
{
hasPlayer = false;
}
if(hasPlayer && Input.GetMouseButtonDown(0))
{
GetComponent<Rigidbody>().isKinematic = true;
transform.parent = cam;
isHold = true;
}
if(toch)
{
GetComponent<Rigidbody>().isKinematic = false;
transform.parent = null;
isHold = false;
toch = false;
}
if(isHold && Input.GetMouseButtonDown(1))
{
GetComponent<Rigidbody>().isKinematic = false;
transform.parent = null;
isHold = false;
GetComponent<Rigidbody>().AddForce(cam.forward * throwForce);
}
else if(Input.GetMouseButtonUp(0))
{
GetComponent<Rigidbody>().isKinematic = false;
transform.parent = null;
isHold = false;
}
}
}