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")