- Home /
Problem with shooting while player moves (third person)
So I have player(just a rolling cube) which can shoot bullets, the player has a script for the shooting:
if(Input.GetKeyDown(pc.kc.keys.attack1))
{
Ray ray = pc.cam.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, ~ignoreLayers))
{
GameObject p = Instantiate(projectilePrefab, transform.position, transform.rotation);
p.GetComponent<Rigidbody>().velocity = pc.rb.velocity;
p.GetComponent<ProjectileController>().cm = this;
p.GetComponent<ProjectileController>().damage = damage;
p.GetComponent<ProjectileController>().force = projectileSpeed;
p.GetComponent<ProjectileController>().target = hit.point;
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
}
else
{
GameObject p = Instantiate(projectilePrefab, transform.position, transform.rotation);
p.GetComponent<Rigidbody>().velocity = pc.rb.velocity;
p.GetComponent<ProjectileController>().cm = this;
p.GetComponent<ProjectileController>().damage = damage;
p.GetComponent<ProjectileController>().force = projectileSpeed;
p.GetComponent<ProjectileController>().target = pc.cam.transform.position + (pc.cam.transform.forward * 1000);
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white);
}
}
But when I move/roll the player and shoot at the same time the bullets go everywhere. I tried almost everything and can't find any solution, my player is moving pretty fast and when my player doesn't move it works perfectly find.
Help appreciated.
Do you have a video of the issue? It may help us understand the issue better. Can you also attach the ProjectileController code?
Just from first glance it seems like you are setting the rotation of the projectile to whatever the rotation of the player is, then giving it a force in (what I assume is) the forward direction of the projectile. If the player is rolling then yes I would expect the bullets to go everywhere
Your answer
Follow this Question
Related Questions
Snapping to walls 1 Answer
Tutorial for third person shooter game ?! 0 Answers
Third Person Shooter (TPS) aiming like Uncharted / MaxPayne3 0 Answers
Make Third Person Character Rotate 1 Answer