- Home /
 
A projectile problem... (Mouse click using Ray/Plane) C#
Okay so I am just mashing up a bunch of code that I found over unityAnswers and spinning it to my own creation. Right now I just want it functionable, then I will tweak it around. I have some code, where I commented out probable issues. I have been researching for hours trying to get a simple script to work, but Rays are kicking my butt!!! I really would appreciate a lending hand in pushing me in the right direction!
 case Action.Bolt:
         {
 
             //find mouse position through a plane, vector3.zero == Y position, so change to a practical value or to
                 //move the whole game down. 
             Plane zeroPlane = new Plane( Vector3.up, Vector3.zero );
             Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
             float distance;
             
             if( zeroPlane.Raycast( ray, out distance ) )
             {
 
 
                 //gets mouse position
                     //need to set origin to the character child "Barrel"
                 Vector3 outputPosition = ray.origin + ray.direction * distance;
                 Debug.Log( "Mouse Position(Bullet should fly here after cool down): " + outputPosition  );
 
                 //spawns bullet
                 Instantiate(ProjectilePrefab, transform.position,Quaternion.identity);
     //cant find a way to add force to above line
         //also need to find a way to get barrel to point in this direction
                 //and make projectile's origin on the character's child "Barrel".
 
 
                 //You can use this position to rotate the bullet like so
                 ProjectilePrefab.transform.LookAt( outputPosition );
         
 
             }
 
             break;
             // Add a case here
             }
 
               I will include code without comments as well. I need to set the origin on the ProjectilePrefab to the child of my character called "Barrel" and then I need to add force to it in the direction of the mouse where I already have. Also that vector3.zero would help if anybody knows.
 case Action.Bolt:
         {
 
             
             Plane zeroPlane = new Plane( Vector3.up, Vector3.zero );
             Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
             float distance;
             
             if( zeroPlane.Raycast( ray, out distance ) )
             {
 
 
                 
                 Vector3 outputPosition = ray.origin + ray.direction * distance;
                 Debug.Log( "Mouse Position(Bullet should fly here after cool down): " + outputPosition  );
 
                 //spawns bullet
                 Instantiate(ProjectilePrefab, transform.position,Quaternion.identity);
     
                 
 
 
                 
                 ProjectilePrefab.transform.LookAt( outputPosition );
         
 
             }
 
             break;
             // Add a case for each Action
             }
 
               Any help will be MUCH appreciated!!!
Distance is... how much?
'out' is for the hitinfo that is created. If you are trying to use distance, its a value you put in to say how far the ray goes. Read the docs
Your answer