Unity C# Casual Jewel Mining Game - Jewel Unaffected by Claw
Hello,
I am following the Unity Live Training tutorial (https://www.youtube.com/watch?v=QvZ6Q3TmoRI) and I can't get the Claw to grab the Jewel game objects. The Claw works on the Rock game object just fine. The raycaster will reach the jewel, but retract as if blocked (like the barrier below). My goal is to have the claw grab the game object, becoming the parent of that game object, and destroying it once it reaches the Origin point.
I tagged the jewels "Jewel" and wrote the following scripts:
For the shooting and raycasting:
using UnityEngine;
using System.Collections;
public class Gun : MonoBehaviour {
public GameObject claw;
public bool isShooting;
public Animator minerAnimator;
public Claw clawScript;
void Update ()
{
if (Input.GetButtonDown("Submit") && !isShooting)
{
LaunchClaw ();
}
}
void LaunchClaw()
{
isShooting = true;
minerAnimator.speed = 0;
RaycastHit hit;
Vector3 down = transform.TransformDirection (Vector3.down);
if (Physics.Raycast (transform.position, down, out hit, 100))
{
claw.SetActive (true);
clawScript.ClawTarget (hit.point);
}
}
public void CollectedObject()
{
isShooting = false;
minerAnimator.speed = 1;
}
}
and this for the Claw game object to interact with the Jewel game objects:
using UnityEngine;
using System.Collections;
public class Claw : MonoBehaviour {
public Transform origin;
public float speed = 4f;
public Gun gun;
public ScoreManager scoreManager;
private Vector3 target;
private int jewelValue = 100;
private GameObject childObject;
private LineRenderer lineRenderer;
private bool hitJewel;
private bool retracting;
void Awake()
{
lineRenderer = GetComponent<LineRenderer> ();
}
void Update()
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards (transform.position, target, step);
lineRenderer.SetPosition (0, origin.position);
lineRenderer.SetPosition (1, transform.position);
if (transform.position == origin.position && retracting)
{
gun.CollectedObject ();
if (hitJewel)
{
scoreManager.AddPoints (jewelValue);
hitJewel = false;
}
Destroy (childObject);
gameObject.SetActive (false);
}
}
public void ClawTarget(Vector3 pos)
{
target = pos;
}
void OnTriggerEnter(Collider other)
{
retracting = true;
target = origin.position;
if (other.gameObject.CompareTag ("Jewel"))
{
hitJewel = true;
childObject = other.gameObject;
other.transform.SetParent (this.transform);
}
else if (other.gameObject.CompareTag("Rock"))
{
childObject = other.gameObject;
other.transform.SetParent (this.transform);
}
}
}
Any help would be greatly appreciated. Thanks in advance.
Answer by Korudo · Dec 23, 2015 at 12:06 PM
If anyone reads this in the future, I figured it out. I had to tag the Mesh for the Jewels as "Jewel", not just the Jewel game objects. :)