- Home /
Getting the Rigidbody of a Collider?
I have a Cube that is a Trigger. And a Sphere with a Rigidboy. I want the Sphere( that has a force applied (1.0f,0.0f,0.0f) ) to move in the Y plane when it enters the Cube.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GoingUp : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
Vector3 movement = new Vector3(0.0f, 5.0f, 0.0f);
other.attachedRigidbody.AddForce(movement);
}
}
But when it applies the vector movement the Sphere doesn't move in the Y plane. What should I do?
Which object is this script a component of, and which object is does the Collider 'other' correspond to?
Answer by Cassos · May 02, 2020 at 05:50 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GoingUp : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
Rigidbody _rb = other.attachedRigidbody; // Get the Rigidbody
if (!_rb) // Check if there is a Rigidbody
return; // Return if there is none
Vector3 movement = new Vector3(0.0f, 5.0f, 0.0f);
rb.velocity = Vector3.zero; // Resets all movements
rb.AddForce(movement); // Apple movement
}
}
Your answer
Follow this Question
Related Questions
Rigidbody.SweepTest Hit Trigger Colliders 0 Answers
How can I access a collider's parent's rigidbody? 1 Answer
How do I disable all rigidbodies and colliders in a scene in unity? 2 Answers
Do continuous and continuous dynamic collision detection work with trigger colliders? 1 Answer
col.gameObject.layer is not working 1 Answer