Can someone please let me know what is happening to my gameobject after it hits a collider it starts to ignore any input.
The code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementControl : MonoBehaviour
{
public float moveSpeed = 5f; // Adjust the movement speed as needed
public float rotationSpeed = 90f; // Adjust the rotation speed as needed
public float maxRotation = 45f; // Maximum rotation angle in degrees
public float minRotation = -45f; // Minimum rotation angle in degrees
private Rigidbody2D rb;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
// Rotation input
float rotationInput = Input.GetAxis("Vertical"); // You can use a different input axis for rotation if needed
float rotationAmount = rotationInput * rotationSpeed * Time.deltaTime;
rb.rotation += rotationAmount;
// Limit rotation to the specified range
rb.rotation = Mathf.Clamp(rb.rotation, minRotation, maxRotation);
// Convert rotation to direction
Vector2 direction = new Vector2(Mathf.Cos(rb.rotation * Mathf.Deg2Rad), Mathf.Sin(rb.rotation * Mathf.Deg2Rad));
// Movement input
float horizontalInput = 1f;
Vector2 movement = new Vector2(horizontalInput * moveSpeed, rb.velocity.y);
// Apply movement in the direction of rotation
Vector2 newVelocity = direction * movement.x;
rb.velocity = newVelocity;
}
}
I’ll guess that your Rigidbody is actually spinning and that your clamp code is holding it at a certain min/max 45-degree angle.
Your inputs are probably not strong enough to counteract the spinning, so each frame your inputs try to “unspin” it and it just respins that frame back to the max / min.
So two things:
set the rb.angularVelocity to zero each frame
do not set the rotation directly. Call rb.MoveRotation() instead. Here’s why:
With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.
This means you may not change transform.position, transform.rotation, you may not call transform.Translate(), transform.Rotate() or other such methods, and also transform.localScale is off limits. You also cannot set rigidbody.position or rigidbody.rotation directly. These ALL bypass physics.
Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.