Trying to shoot object at a certing point via force
I'm trying to make a 2D game, where you are a mage and you can shoot on any direction you want. In my current situation, I want that the bullet will go to the crosshair direction, but its not working for me, it goes to anywhere except the cursor. Here my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shoot : MonoBehaviour
{
//Will shoot bullet relative to wand end point
[Header("Vars")]
[SerializeField]private Rigidbody2D Bullet;
[SerializeField]private Transform ts;
[SerializeField]private Transform barrell;
[SerializeField]private Transform Player;
[SerializeField]private float thrurst;
//raiz(x2-x1)^2 + (y2-y1)^2
private bool Mouse;
float x1,x2, y1, y2, result, rootX, rootY;
//Vector2 PlayerPos = new Vector2(Player.position.x, Player.position.y);
private void Update()
{
GetMouseInput();
if(Mouse)
{
var newBullet = Instantiate(Bullet, barrell.position, Quaternion.identity) as Rigidbody2D;
float angle = GetAngle();
var Force = findFuckingForces(angle);
newBullet.AddForce(Force * thrurst, ForceMode2D.Impulse);
//newBullet.AddRelativeForce(Vector3.right * thrurst, ForceMode2D.Impulse);
}
}
void GetMouseInput()
{
Mouse = Input.GetMouseButtonDown(0);
}
//It is supposed to find the adjacent side and opposite side, then return it as Vector 2 to pass as instantiate
Vector2 findFuckingForces(float angle)
{
//Gets the hypotenuse the shortest path to a point
x1 = Player.position.x;
x2 = ts.position.x;
y1 = Player.position.y;
y2 = ts.position.y;
Vector2 Pos;
rootX = Mathf.Pow((x2 - x1), 2);
rootY = Mathf.Pow((y2 - y1), 2);
result = Mathf.Sqrt(rootX + rootY);
//acha cateto oposto e adjacente
//cosseno cateto adjacente angulo = cateto adjacente / hipo
//seno cateto oposto angulo = cateto oposto / hipo
float coss = Mathf.Cos(angle);
float seno = Mathf.Sin(angle);
float catetoAdj = coss * result;
float catetoOpst = seno * result;
Pos = new Vector2(catetoOpst, catetoAdj).normalized;
return Pos;
}
//it is Supposed to return an angle between end of wand and where the crosshair is
float GetAngle()
{
x1 = Player.position.x;
x2 = ts.position.x;
y1 = Player.position.y;
y2 = ts.position.y;
float angle = Mathf.Atan2(y2 - y1, x2 - x1) * 180 / Mathf.PI;
Debug.Log(angle);
return angle;
}
}
` I tried using some trigonometry but I don't know if I did it right.
Answer by WolverineLight · May 27 at 03:48 PM
Hello MrPanda05,
Firstly I would suggest NOT using AddForce, or any kind of active physics like this, to propel a bullet. It is possible, however. Some alternatives include A) RayCast, which can be used to scan for a bullet impact, or B) is to continually move the bullet every frame. If you game requires a physics-based bullet, it may be best to do the originall physics-based approach.
Secondly, as a solution, there are a few ways to approach this.
Using physics, you can do the following:
Create two empty gameobjects.
Place one to be a child of the main camera. This will act as an origin.
Place the other in the direction you wish to aim, (also on the main camera).
Use the positions of both of these to create a direction. (Simply subtract the positions of the two for a direction).
To fire, instantiate the bullet at the origin. Add a force in the direction you calculated above. No trig necessary.
Using raycast: - Simply create a raycast:
var ray = camera.ScreenPointToRay (Vector3 (Screen.width/2,Screen.height/2, 0));
Check for a collision:
var hit : RaycastHit; if (Physics.Raycast (ray, hit, 100)) { distance = hit.distance; collider1= hit.collider; Debug.Log(distance); Debug.DrawLine(transform.position, hit.point, Color.red); }
If you want to move the bullet slowly, simply moving it every frame might work.
You can also break raycast into sections if you want to have the bullet travel over time as well.