.rotation doesn't seem to work, can't shoot projectile at mouse coordinates
I'm trying to get a projectile to work, but it won't go the direction I click. I have debug.drawline showing if my mouse coordinates are working and the line works fine and follows my mouse. Can anyone tell me what is wrong with my code?
//Variables used here //public LayerMask WhatToHit; //public int lifeTime = 100; //Transform firePoint; //public Transform bulletTrailPreFabEffect;
private void shoot() { //Debug.LogError("Mouse pos X " + Camera.main.WorldToScreenPoint(Input.mousePosition).x + " Mouse Y coord: " + Camera.main.WorldToScreenPoint(Input.mousePosition).y); //Debug.LogError("Function is working"); //Mouse position Vector2 mousePos = new Vector2( Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y); //Fire point position Vector2 firePointPos = new Vector2(firePoint.position.x, firePoint.position.y);
//Get our direction
Vector2 direction = mousePos + firePointPos;
//Normalize our direction
direction.Normalize();
//Debug.LogError("The direction is " + direction);
RaycastHit2D hit = Physics2D.Raycast(firePointPos, direction, lifeTime, WhatToHit);
//Bullet effect
Instantiate(bulletTrailPreFabEffect, new Vector3(firePoint.position.x, firePoint.position.y, firePoint.position.z), firePoint.rotation);
//Debug to see if it works
Debug.DrawLine(firePointPos, mousePos, Color.black);
//Debug to see if we hit something
//if (hit.collider != null)
//{
// Debug.DrawLine(firePointPos, hit.point, Color.red);
// Debug.Log("We hit" + hit.collider.name + " And it did " + damage + " Damage");
//}
}
Can't seem to get the full code in that little window, sorries! If anyone knows how to make it fit, let me know so I can edit it
Also did some more testing: Debug.LogError("Rotation: " + firePoint.rotation + " Firepoint Pos : " + firePoint.position.x + ", <- x y -> " + firePoint.position.y);
When I do that, my rotation is always 0,0,0,0 and my x/y won't move until I move my character