- Home /
Script adds rigidbody but player can walk straight through the gameobject?
I have a scene in which the player is inside a house and the walls fly away when the player collides with them, this works well however I want the Rigidbody to be added UPON the collision, if I simply add it in the editor all of the walls fly away when the scene begins! I have the script below which adds the rigidbody however the player just walks straight through the wall, why would this occur? I have another wall in which I have manually added the rigidbody and it works fine......
using UnityEngine;
using System.Collections;
public class OnCollisionAddPhysics : MonoBehaviour {
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
Rigidbody newRigidBodyComponent = gameObject.AddComponent<Rigidbody>();
gameObject.GetComponent<Rigidbody>().useGravity = false;
Debug.Log("collided!");
}
}
This screenshot shows a wall that behaves as I intend however I have manually added the rigidbody
This screenshot shows a wall in which the rigidbody is added via my script and the player walks straight through it
Thanks in advance to any assistance!!
Answer by Grench1 · Jan 01, 2017 at 09:03 PM
In the script change the defaul value "isKinematic" to true. The script should look like this:
using UnityEngine;
using System.Collections;
public class OnCollisionAddPhysics : MonoBehaviour {
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
Rigidbody newRigidBodyComponent = gameObject.AddComponent<Rigidbody>();
gameObject.GetComponent<Rigidbody>().useGravity = false;
**gameObject.GetComponent<Rigidbody>().isKinematic = true;**
Debug.Log("collided!");
}
}
Your answer
Follow this Question
Related Questions
Is it recommended to do scientific modelling in unity or should i write a standalone program 3 Answers
Car Movment Script not fully working!!!!!!! 1 Answer
Need help with my movement script.. :( 1 Answer
2D Platformer with custom physics Object - jumping issue 0 Answers
Model not translating along its position with Animate Physics option enabled. 0 Answers