Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mchalo · May 28, 2011 at 03:24 AM · fpsray

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 :) MCHALO

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Antony-Blackett · May 28, 2011 at 04:07 AM

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

 ray.direction = cameraObject.transform.forward;
 ray.origin = bulletSpawnNode.position;
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mchalo · May 28, 2011 at 04:18 AM 0
Share

but would that not make the bullet come out from the came ? i want my bullet to shoot out from the front of my gun :). if was to use your method should i create a new script and then attach it to the cam. but even then how do i instantiate the bullet out of the ray???

avatar image Antony-Blackett · May 28, 2011 at 05:02 AM 0
Share

set the origin to where you want. I edited my answer to show this.

avatar image testure · May 28, 2011 at 05:31 AM 0
Share

-edit: moved it to an answer so i could use the code blocks

avatar image mchalo · May 28, 2011 at 11:36 PM 0
Share

i am really sorry about this but i am not that good at program$$anonymous$$g yet where in my script do i put these lines of code. and on which script do i put it on? $$anonymous$$y players FPS input controllers script or the Ray script that i have created ?. I have tried but i have failed i created a new var called ray and then defined it as a Ray is that correct. If you could can you please do it for me in my script so i can see how it is done

i would be really thank full :) cheers

avatar image
0

Answer by testure · May 28, 2011 at 05:32 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rayshoot Reload Problem (Can still shoot) 2 Answers

Got confused about logic over an FPS game. Gun pointing, laser sight, raycasting etc. 1 Answer

Rotate From SpawnPoint TO Transform Rotation 1 Answer

Raycast hitting below mouse position :( 0 Answers

is there a way to get a mdl in untiy 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges