Question by
SimonRupov · Jan 29, 2020 at 02:28 PM ·
c#scripting problem3d
My Object Picking up script using / selecting more than one object!
Using Tutorial in YouTube I wrote this code (Modified with TempParent):
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PickObj : MonoBehaviour
{
public Vector3 position;
public Transform player;
public Transform Guide;
public Transform playerCam;
public float throwForce = 10;
bool hasPlayer = false;
public KeyCode TKey;
bool beingCarried = false;
public AudioClip[] soundToPlay;
private AudioSource audio;
public int dmg;
private bool touched = false;
void Start()
{
audio = GetComponent<AudioSource>();
}
void Update()
{
float dist = Vector3.Distance(gameObject.transform.position, player.position);
if (dist <= 2.5f)
{
hasPlayer = true;
}
else
{
hasPlayer = false;
}
if (hasPlayer && Input.GetKeyDown(TKey))
{
GetComponent<Rigidbody>().isKinematic = true;
transform.parent = Guide;
transform.position = Guide.transform.position;
beingCarried = true;
}
if (beingCarried)
{
if (touched)
{
GetComponent<Rigidbody>().isKinematic = false;
transform.parent = null;
beingCarried = false;
touched = false;
}
if (Input.GetMouseButtonDown(0))
{
GetComponent<Rigidbody>().isKinematic = false;
transform.parent = null;
beingCarried = false;
GetComponent<Rigidbody>().AddForce(playerCam.forward * throwForce);
RandomAudio();
}
else if (Input.GetMouseButtonDown(1))
{
GetComponent<Rigidbody>().isKinematic = false;
transform.parent = null;
beingCarried = false;
}
}
}
void RandomAudio()
{
if (audio.isPlaying){
return;
}
audio.clip = soundToPlay[Random.Range(0, soundToPlay.Length)];
audio.Play();
}
void OnTriggerEnter()
{
if (beingCarried)
{
touched = true;
}
}
}
When I add this script to the object, I select Cam, Player, and Guide. With single objects script works fine, object goes to the center of the screen (Goes to the Guide), when I place more objects, they selecting all at once. Please somebody help, maybe I need to use Raycast? Or something else?
[1]: /storage/temp/151991-screenshot-268.png
[1]: /storage/temp/151991-screenshot-269.png
screenshot-268.png
(89.4 kB)
screenshot-269.png
(39.7 kB)
Comment