- Home /
Question by
unity_FAOJNiBt8yiFGg · Sep 09, 2019 at 04:37 PM ·
rigidbody2daddforcemousepositionknockback
AddForce knockback x axis according to mouse.position more then y axis (2D game)
(2D game) So my player has an Addforce type of a knock-back in the opposite direction of a mouse position after pressing fire1 button (shotgun effect), and it works, but I want to add more force when player is shooting with mouse position in x axis, so that it is more knockback on sides rather then top and bottom. Player In combination with jumping mechanic knock'sback to far away in y axis.
Vector2 direction = Rigidbody2D.transform.position - firePoint.transform.position;
Rigidbody2D.AddForce(direction * knockback);
Vector2 mousePosition = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
//the lines bellow this sentence does not work, becouse mouse position.x is float, not bool, but thats what I am thinking right now
if (Input.mousePosition.x = true) {
//addforce more then public float which is 600
}
any solution is great, but what I am trying to achieve this month is dynamic addForce, meaning the closer mouse position is to true x axis the more power and further top and close to y axis less power. It is a bit complicated, but I bet it is posibble :), anything is possible
Comment