Problems when firing projectiles towards cursor
Good evening.
I'm trying to implement the logic behind firing projectiles from the main character, who is placed at a fixed position on the left side, towards my mouse position (later to be changed to touch position as my target platform is android). What i'm trying to achieve is simple and after some researches the closest i could get to it is the following:
using UnityEngine;
public class FireProjectile : MonoBehaviour
{
public Rigidbody2D projectile;
public float speed = 8f;
void Update()
{
if (Input.GetButton("Fire1"))
{
var projectileInst = Instantiate(projectile, transform.position, Quaternion.identity);
var hitPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
projectileInst.transform.LookAt(hitPoint);
projectileInst.GetComponent<Rigidbody2D>().AddForce(projectileInst.transform.forward * 1000);
}
}
}
It's firing correctly towards the cursor, however the velocity varies depending on how far the cursor is from the origin (the character on the left), as shown in these images:
1) Cursor on the extreme right: https://imgur.com/nmsC9ap
2) Cursor above the character head: https://imgur.com/491RuSX
It's driving me mad, the only other solution that came to mind was forgetting about the physic engine and just manage movements manually via Transform, however this can introduce instances where collisions aren't detected...
Your answer
Follow this Question
Related Questions
WheelJoint2D rotates parent object 1 Answer
Help 2D Custom HillClimbRacingGame 0 Answers
2D Accuracy 0 Answers
Player controlled platforms 0 Answers
Getting rid of 2D collision jitter 1 Answer