Question by
katberymc · Nov 30, 2019 at 09:27 AM ·
c#instantiateraycastprefabraycasthit
Raycast hit not detection gameobject?
Hi, I have an gameobject. And I move this gameobject to a certain point.When a certain distance passes a new prefab instance.This gameobject stops when it reaches the ontriggerenter point.
Until this stage there is no problem.
there is a raycast in the new instance prefab.I want this raycast to stop when it hits the gameobject in back.
void FixedUpdate() {
rb.MovePosition (transform.position + transform.forward * speed * Time.fixedDeltaTime );
RaycastHit hit;
Vector3 forward = transform.TransformDirection(Vector3.forward) * 30;
Vector3 back = transform.TransformDirection(-Vector3.forward) * 30;
Debug.DrawRay(transform.position, forward, Color.yellow);
Debug.DrawRay(transform.position, back, Color.blue);
if (Physics.Raycast (transform.position,(forward), out hit)) {
if (hit.distance <= 10.0f && hit.transform.tag == "apple") {
rb.constraints = RigidbodyConstraints.FreezeAll;
}
else {
rb.MovePosition (transform.position + transform.forward * speed *
Time.fixedDeltaTime );
rb.constraints = RigidbodyConstraints.None;
}
}
}
If the gameobject in front is gone, let it continue to move.
error.png
(32.9 kB)
Comment