- Home /
FPS shooting Help!
Hi guys. i was wondering if someone could help me out with something. Basically i just created simple lines of code which instantiate bullets from my gun. but the problem is when i run the game and shoot straight it shoots fine the bullets go straight. But if i start moving my gun up and down the bullets react funny. Basically if i look up and shoot the bullets start to shoot but fall down they dont shoot straight, and if i am facing down the bullets shoot and go up. now that is unrealistic to me. can some one please show me how i can shoot out of a raycast instead of instantiating from a spawn point in front of the gun. this way the bullets will fallow the ray in a straight direction and then fall later on.
all i did was i added the unity fps control system and in the FPSInputController i added the instantiate bullet code. how can i do this using a ray cast instead. Thank you very very much in advance MCHALO looking forward to your guys replies thanks
Here is the script:
private var motor : CharacterMotor; var Bullet_Shoot: Transform; var BulletSpeed : float; // Use this for initialization function Awake () { motor = GetComponent(CharacterMotor); //Bullit_Shoot = transform; }
function Start (){
BullitSpeed = 20000;
}
// Update is called once per frame function Update () { // Get the input vector from kayboard or analog stick var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if (directionVector != Vector3.zero) {
// Get the length of the directon vector and then normalize it
// Dividing by the length is cheaper than normalizing when we already have the length anyway
var directionLength = directionVector.magnitude;
directionVector = directionVector / directionLength;
// Make sure the length is no bigger than 1
directionLength = Mathf.Min(1, directionLength);
// Make the input vector more sensitive towards the extremes and less sensitive in the middle
// This makes it easier to control slow speeds when using analog sticks
directionLength = directionLength * directionLength;
// Multiply the normalized direction vector by the modified length
directionVector = directionVector * directionLength;
}
if(Input.GetKey(KeyCode.Z)){
var Bullet = Instantiate(Bullet_Shoot, GameObject.Find("Bullet _Shoot").transform.position, Quaternion.identity); Bullit.rigidbody.AddForce(transform.forward BulletSpeed Time.deltaTime); print("HHHHH");
} // Apply the direction to the CharacterMotor motor.inputMoveDirection = transform.rotation * directionVector; motor.inputJump = Input.GetButton("Jump"); }
// Require a character controller to be attached to the same game object @script RequireComponent (CharacterMotor) @script AddComponentMenu ("Character/FPS Input Controller")
Answer by Dave 11 · May 09, 2011 at 12:28 PM
i know how to ray cast but how do i instantiate it co$$anonymous$$g from the ray itself ?
basically at the moment i press a key and then it instantiates the bullet, and adds force to it. But the problem is when i shoot up the bullets fall start to fall down because rigidboady is applied to them. and if i face down and shoot the bullets dont shoot down. what i want is i created a blank object where the bullet instantiates from.n But what i want is on that blank object an ray cast shooting out straight. i then need to some how make the bullets instantiate from the bullet re-spawn but this time it should shoot from the ray in a straight line.if this is confusing please tell me
then i will update the answer with images on what i am talking about :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613132849im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Shooting with a locked camera position 1 Answer
continuous shooting 1 Answer
Make multiple Projectiles move at the same Time... 0 Answers
FPS Kit Version 2.0 1 Answer
Object Instantiating to many objects 1 Answer