- Home /
Raycast hit info not registering on specific gameobject
Hello, I've been trying to do a tree system, it all works fine, it registers if the tag is "Tree" and works for the trees. However when the trees do the SpawnLogs() function that spawns a log, when I left click on the instantiated log object with the tag "Log" it doesn't do anything, it doesn't register at all.
Is this something to do with the instantiate part? Why isn't hitinfo registering the log the tree spawns?
Tree script that spawns the log:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Trees : MonoBehaviour
{
[Header("Tree Settings")]
public float treeHealth = 100f;
public GameObject logs;
GameObject thisTree;
GameObject player;
Rigidbody rb;
private void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
thisTree = this.gameObject;
rb = thisTree.GetComponent<Rigidbody>();
rb.isKinematic = true;
}
private void Update()
{
if(treeHealth <= 0)
{
rb.isKinematic = false;
rb.AddForce(player.transform.right * 8);
StartCoroutine(DestroyTree());
}
}
private IEnumerator DestroyTree()
{
yield return new WaitForSeconds(5);
Destroy(thisTree);
SpawnLogs();
}
private void SpawnLogs()
{
Vector3 position = new Vector3(Random.Range(-1.0f, 1.0f), 0, Random.Range(-1.0f, 1.0f));
Instantiate(logs, thisTree.transform.position + new Vector3(0, 0, 0) + position, Quaternion.Euler(90, 0, 90));
}
}
Axe script that gets the hitInfo:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Wooden_Axe : MonoBehaviour
{
float x = 87.4f;
float y = 241.13f;
float z = 4.5f;
GameObject player;
public float attackDistance = 5f;
public float minDamage = 30f;
public float maxDamage = 5f;
public float eyeHeight;
void Start()
{
player = GameObject.FindGameObjectWithTag("MainCamera");
transform.localPosition = new Vector3(0.55f, -0.45f, 1);
transform.localRotation = Quaternion.Euler(x, y, z);
}
void Update()
{
// Raycast
Vector3 eyePosition = new Vector3(player.transform.position.x, player.transform.position.y + eyeHeight, player.transform.position.z);
Ray ray = new Ray(eyePosition, player.transform.forward);
RaycastHit hitInfo;
Debug.DrawRay(ray.origin, ray.direction * attackDistance, Color.red);
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray, out hitInfo, attackDistance))
{
if(hitInfo.collider.tag == "Tree")
{
float axeDamage = Random.Range(minDamage, maxDamage);
Trees treeScript = hitInfo.collider.gameObject.GetComponent<Trees>();
treeScript.treeHealth -= axeDamage;
}else if(hitInfo.collider.tag == "Log")
{
float axeDamage = Random.Range(minDamage, maxDamage);
Logs logScript = hitInfo.collider.gameObject.GetComponent<Logs>();
logScript.logHealth -= axeDamage;
}
}
}
}
}
Is it to do with the Instantiate in the Tree script that instantiates the log? Because the log has the tag and a box collider so I see no reason why it's not working, unless I'm being dumb and missing something...
Hello, @$$anonymous$$istyisty123 ! I suggest you to check if your log prefab has a collider on a log object (not on his children). Because if you added collider to log children, then your hitInfo.collider is a children, not a root log object.
Also, do you see spawned log object in scene? and in hierarhy tree too? is it has position exactly where it must be?
@$$anonymous$$ennai yes the prefab has a box collider on it, it has no children. When the log is spawned it is in the scene yes and is in the heirarhy tree as well. Position is all correct.
Your answer
Follow this Question
Related Questions
Make tagged objects explode with ray cast? 1 Answer
mouse position on tag 1 Answer
find GUIText 1 Answer
Raycast starts ignoring gameobject after a few seconds 0 Answers
Make an instantiated object match the slope of the terrain 1 Answer