Help with shooting using raycast !!!!!!

hi all, i was wondering if someone could help me. Basically i am creating a fps game i just made my gun start shooting and i thought everything was ok but then i ran into a problem. the problem is that if i look up and shoot the bullets don’t shoot up they keep on shooting straight even tough i am looking up. this is the same for if i am looking down.
To make this more easy to understand i have made screen shoots explaining what is going on.


Image 1


basically in this image i am just showing you where the bullet spawns from.
if for some reason the image is not viewable please click on the direct link


Direct link :

http://www.imagehosting.com/photo/albums/unity-3d


alt text


Image 2:


Now in this image if i am basically showing the gun shooting straight. which is all working fine.

image :


alt text


Ok as you can see in the image the bullet is shooting straight, but if i was to shoot up i will not work right. Basically if you look up and shoot the bullets should fire up as well. but in this case they don’t if i look up and shoot the bullets will still remain in the centre this is happening if i look down as well.

here is the image showing you what i am talking about:

Image:


alt text


Now i have had an idea on how to prefect this idea, basically instead of instantiate it from the bullet spawn gameobject i will send the bullet flying out form a ray. so the next time i shoot when it instantiates my bullet it will always go in the direction of the ray.

this is what i have done so far.
basically i have created a script for the spawn point which shoots out a ray in the forward direction of the gun.
here is the script :

var spawn: GameObject;
function Update () {
//var hit : RaycastHit;
   var Fwd = transform.TransformDirection(Vector3.forward);
   
if(Input.GetMouseButton(0)){

Debug.DrawRay(spawn.transform.position, Fwd * 10 , Color.red);
      if(Physics.Raycast(transform.position, Fwd , 10)){
      
          
      
      
      }
      else{
      
           //Debug.DrawRay(spawn.transform.position, Fwd * 10 );
      
      }

}
}

this script is attached to my spawn point. basically all it does is it says if i click the mouse button (0) the draw a line ray in front of the spawn object. Now once the spawn object created the ray i want is to instantiate a bullet out of that ray.

Now i tried this, i opened unity’s "FPS Input controller script " and i added this line :

if(Input.GetMouseButton(0)){
	
	   
	var Bullet = Instantiate(Bullet_Shoot, GameObject.Find("Spawn_point ").transform.position, Quaternion.identity);

   Bullit.rigidbody.AddForce(transform.forward * 200000);
   print("HHHHH"); 

Here is the full script if you need to have a look:


FPS Input Controller 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 (){


}

// 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.GetMouseButton(0)){
	
	var Bullit = Instantiate( Bullet_Shoot, GameObject.Find("spawn_point").transform.position, Quaternion.identity);

   Bullet.rigidbody.AddForce(transform.forward * 200000);
   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")

basically every time i now click the 0 button on the mouse the spawn point will first make a ray and then instantiate a bullet. but that bullet will not fallow the ray how do i get it to shoot it out of the ray ?

i would be really happy and great full if someone could help me i would mean alot.

thank you so much in advance :slight_smile: MCHALO

You need to cast your ray in the direction of the camera’s forward direction.

ray.direction = cameraObject.transform.forward;
ray.origin = bulletSpawnNode.position;

you can also fire rays directly from screenspace, so if you have a crosshair you can fire it directly out of the center of the crosshair and use the camera.transform.forward as the direction.

for example, if you wanted to put a crosshair in the middle of the screen and fire it at your ‘target’, you’d do something like this:

// C# by the way, but should be easy to convert to JS

Ray ray = camera.ScreenPointToRay(
                  new Vector3(Camera.mainCamera.pixelWidth/2f,
                  Camera.mainCamera.pixelHeight/2f, 0));
Debug.DrawRay(ray.origin, Camera.mainCamera.transform.forward * 10, Color.red); 

// cast your ray the same way.