- Home /
How do I stop a rigidbody from rotating after colliding with another object?
Good Day guys, i want to create a game like geometry dash the rigidbody runs by itself but when it hits the ground it starts to rotate
//code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour { public Rigidbody2D rb; public Transform groundCheck; public float groundCheckRadius; public LayerMask whatIsGround; private bool onGround;
// Start is called before the first frame update
void Start() {
rb = GetComponent <Rigidbody2D>();
}
// Update is called once per frame
void Update() {
rb.velocity = new Vector2(4, rb.velocity.y);
onGround = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
if (Input.GetMouseButtonDown(0) && onGround)
{
rb.velocity = new Vector2(rb.velocity.x, 6);
}
}
}
Answer by Frostoise · Nov 10, 2020 at 10:58 PM
If you want your gameobject to not rotate at all, you can open Rigidbody2D > Constraints and Freeze Rotation.
If you want to do it on runtime with code:
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
//Stop the rotation.
rb.constraints = RigidbodyConstraints2D.FreezeRotation;
//Continue the rotation.
rb.constraints = RigidbodyConstraints2D.None;
}
}
This should enable you to control when your object rotates.
Well actually first one was what I searched for. Thankyou very much:)
Your answer
Follow this Question
Related Questions
How to rotate a Rigidbody2D faster than 4500 (degrees/s)? 0 Answers
Trying to clamp the movement of a rigidbody2D 1 Answer
unity2d cannot create if gravity level 1 Answer
No changing Linear Drag (.linearDrag) on rigidbody / rigidbody2D in code? 1 Answer
Will centerOfMass rotate my Rigidbody2D with gravity. 0 Answers