- Home /
When Player Collide with Cube, Cube changes to Rigidbody 2D
Hey everyone, I'm just creating a platform game where you are a cube and you have to pass the level, there are many Enemies on the level and I would like that if Player touches Enemy this becomes physical and Rigidbody2D (Dynamic) will turn on.
Here is my script that is in Player but it doesn't work, can anyone know how to fix it?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
private Rigidbody2D rb;
public class EnableComponents : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
Rigidbody2D.isDynamic = false;
}
// Update is called once per frame
void Update()
{
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Enemy")
{
Rigidbody2D.isDynamic = true;
}
}
}
}
Answer by ATU15 · Apr 26, 2020 at 06:21 PM
First you should declare the variable "private Rigidbody2D rb;" in the "EnableComponents : MonoBehaviour"- class. Second you can't write "void OnCollisionEnter2D(Collision2D col) { }" in a "void Update ()". Try this out:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnableComponents : MonoBehaviour
{
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
Rigidbody2D.isDynamic = false;
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Enemy")
{
Rigidbody2D.isDynamic = true;
}
}
}
This dosen't work, because "Rigidbody 2D" does not contain definention for "isDynamic" but never$$anonymous$$d i just make box colider smooth and its working like i want :D
You are right. Rigidbody2D has not a definition for "isDynamic" but for "Dynamic".
Your answer
Follow this Question
Related Questions
OnTriggerEnter2D(Collider2D other) 2 Answers
Handling friction in scripts for a 2D platformer 1 Answer
Unity 2D player sticks on platform corners 2 Answers
Way to achieve 3 lane mechanism in a 2D game 0 Answers
2D Wall Jump and Wall Friction 0 Answers