How to get the Collider other element Rigidbody?
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?
Answer by streeetwalker · May 02, 2020 at 10:19 AM
Hi @BullseyeInformatica, You need to be more specific. There is no 'Y" plane. There is the xy plane, and there is the yz plane. and there is the xz plane.
Perhaps you mean only on the Y axis?
If that is the case, your Sphere is already moving on the x axis. Without any damping or other opposing force, it will keep moving forever in the direction of the force you applied. Remember Newtons first Law of Motion: "Every body persists in its state of being at rest or of moving uniformly straight forward, except insofar as it is compelled to change its state by force impressed."
Therefore, applying another force on another axis (",,,change its state by force impressed" ) will cause it to move at an angle - it does not cancel out the previously applied force.
if you want it to stop it's current motion, do this:
other.attachedRigidbody.velocity = new Vector3.zero
That will instantly freeze the motion of the sphere. Then you can apply your force along the y axis.
Your answer
Follow this Question
Related Questions
How to get the Collider other element Rigidbody? 0 Answers
Destroy instatiate object on trigger enter / collision,destroy instantiate prefab on trigger enter 0 Answers
Destroying Object OnTriggerEnter2D not working 1 Answer
Remove Door's collider if cube is in the trigger,Destroy Door's Collider if box is in the trigger 0 Answers
How to add rigidbody to a imported gameobject of a modeling software? 1 Answer