Simple Shooting Question
Hi guys, i was making a little shooting script to test some destructibility stuff and i'm having a little issue with the shooting.
For some reason the bullet won't go where i click on the left, it works fine when i click on the middle or the right but when i click on the left it just shoots towards the center.
Image for Clarification :
So could someone point out the issue thats causing it please.
I have a feeling its the normalise that is doing it but if i don't normalise the direction it messes up the shot completely.
Heres the if statement that handles firing the bullet.
if (Input.GetButton ("Fire1") && canfire == true)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray,out hit, 300))
{
Vector3 pos = hit.point;
GameObject go = Instantiate (bullet, transform.position, Quaternion.identity) as GameObject;
Debug.Log (pos);
Vector3 dir = pos - go.transform.position;
dir.Normalize ();
go.GetComponent<Rigidbody> ().AddForce (dir * force, ForceMode.Impulse);
timer = 2;
canfire = false;
}
}
Answer by Aalnius · Nov 04, 2016 at 05:10 PM
I'm an idiot the projectile was hitting the players collider which was messing up the shot.
it works fine now after stopping collision between the player and the projectile.
I thought that might have been the case, we´ve all made that mistake :)
Answer by Zynek · Nov 04, 2016 at 04:37 PM
What do you mean by "where i click on the left" ? Could you provide some pictures ?
By left i mean if you divide your screen vertically in the middle then the left section.
But heres a picture of my scene to clarify: