- Home /
Question by
mightybob · Jan 19, 2015 at 03:49 AM ·
javascriptshootstraight
How to shoot a puck straight? (JS)
Hello, working on a hockey game, trying to get my player to shoot a puck straight, right now he shoots it, but if he's moving diagonally the puck shoots at a 45 degree angle, and even when he's straight the puck goes a few degrees up or down.
Here is my code:
The shoot puck function:
public function shootPuck(shootPower:Vector3) {
Player = null;
followPlayer = false;
if (shootPower.x != 0) {
rigidbody.velocity.x = shootPower.x;
}
if (shootPower.y != 0) {
rigidbody.velocity.y = shootPower.y;
}
if (shootPower.z != 0) {
rigidbody.velocity.z = shootPower.z;
}
Physics.IgnoreCollision(Player.collider, collider);
}
The code that executes the function:
if (Input.GetKey(KeyCode.Space) && puck.GetComponent(puckScript).Player == gameObject) {
if (pointingRight) {
puck.GetComponent(puckScript).shootPuck(Vector3(5,0,0));
} else {
puck.GetComponent(puckScript).shootPuck(Vector3(-5,0,0));
}
}
And the code that moves the player, just incase that may be affecting anything.
if (Input.GetKey("w"))
{
rigidbody.velocity.z += (speed-rigidbody.velocity.z)/ease;
}
if (Input.GetKey("a"))
{
rigidbody.velocity.x += (-speed-rigidbody.velocity.x)/ease;
transform.localScale.x = -originalScaleX;
pointingRight = false;
}
if (Input.GetKey("s"))
{
rigidbody.velocity.z += (-speed-rigidbody.velocity.z)/ease;
}
if (Input.GetKey("d"))
{
rigidbody.velocity.x += (speed-rigidbody.velocity.x)/ease;
transform.localScale.x = originalScaleX;
pointingRight = true;
}
Any help on this would be great, as I'm stumped right now.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613180816im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
My gun won't fire. ( Javascript ) 1 Answer
Auto shooting script. 1 Answer
I want to be able to make a good shoot script that actually works. 0 Answers
Clones of enemies will not shoot 0 Answers
First person shooter shoot Function 3 Answers