- Home /
Question by
opritaoctav · Dec 12, 2021 at 09:26 PM ·
collisionmousepositiontop down shooter
Shake problem when colliding to walls Top Down (via MousePos)
Hi! so im am making a simple game about a triangle that moves via keyboard and rotates the angle so that is follows the mouse. But when i collide into walls it shakes and i don't like it.. Here s the code and the video. code : public class playerMovement : MonoBehaviour { public float movementSpeed = 10f;
public Rigidbody2D rb;
public Camera cam;
Vector2 movementPos;
Vector2 mousePos;
//input
void Update()
{
movementPos.x = Input.GetAxisRaw("Horizontal");
movementPos.y = Input.GetAxisRaw("Vertical");
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
}
//movement
private void FixedUpdate()
{
rb.MovePosition(rb.position + movementPos * movementSpeed * Time.fixedDeltaTime);
Vector2 lookDir = mousePos - rb.position;
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
rb.rotation = angle;
}
}
and video:video link
Comment
Your answer
Follow this Question
Related Questions
Collision between GUI Texture and Object 1 Answer
How to find out what is the closest point on a object? 0 Answers
How do I move camera towards the mouse while anchoring it to the player? 0 Answers
Change script to make the object only movable in one direction and add collision 0 Answers
Top Down shooter mouse controls 2 Answers