- Home /
When collider, add a rigibody to the gameobject
How i can add a rigibody to a gameobject with C#
i want add a rigibody to a wall when a rock collider with it. it´s like a catapult
The scrip:
using UnityEngine;
using System.Collections;
public class Choque : MonoBehaviour {
void OnCollisionEnter (Collision vColisionando){
if (vColisionando.gameObject.tag == "construccion"){
vColisionando.AddComponent<Rigidbody> ();
}
}
Answer by Nemrav · Jan 28, 2015 at 11:31 PM
thing.gameObject.AddComponent(); you had it, but your problem is going to be that the rigidbody is added when the wall is hit, which means no force is added to your wall. The solution is to cast a ray in the rocks trajectory ('thing.rigidbody.velocity.normalized') and if the ray hits the wall, add the rigidbody and its properties
Answer by Addyarb · Jan 28, 2015 at 11:32 PM
use gameObject.AddComponent();
Just be sure to check to make sure it has a rigidbody first. Though I'm not sure if adding a rigidbody as soon as the rock hits the wall is going to be enough time for physics to respond. I could be wrong - only one way to find out.
Your answer
Follow this Question
Related Questions
Rigidbody Collision? 1 Answer
RigidBody Clings to Walls 0 Answers
Weird bounching with Rigidbodies (2D) 0 Answers
Is there a rigidbody character controller 3rd person that can handel stairs (steps)? 1 Answer
Animator overriding collisions? 0 Answers