Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 seb-lopez · Feb 19, 2015 at 10:52 PM · raycast2d gamemouseposition

shoot to the mouse cursos

Hey guys im trying to do a 2d game shooter ( not spirit, 3d aspect ) , and im trying to change a shooting script where instead of shooting forward , i shoot toward the mouse cursos but i get errors ,here what i tried

 function shoot () {
   
    var Hit : RaycastHit; 
    
        var DirectionRay = transform.TransformDirection(Vector3.forward); // <== (the part i want to change)

         Debug.DrawRay (transform.position ,DirectionRay * Range , Color.blue);
        if (Physics.Raycast(transform.position , DirectionRay , Hit, Range)){
           
           
           if (Hit.rigidbody){
           
               Hit.rigidbody.AddForceAtPosition ( DirectionRay * Force , Hit.point);
               Hit.collider.SendMessageUpwards("ApplyDamage" , Damage, SendMessageOptions.DontRequireReceiver);
        }
        }


i tried with:

 var Directionay = Camera.main.ScreenPointToRay (Input.mousePosition );


but didn't work lots of methode errors =(

Comment
Add comment · Show 2
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 melkorinos · Feb 19, 2015 at 11:55 PM 0
Share

Take a look at this, at 17:40 , he makes an arm rotation script , i guess this is what you are looking for.You can use that rotation value for your shooting script.

And there is a small mistake , line 8: the paranthesis should close just before the semicolon ");" and not just include the input mouse position.

avatar image seb-lopez · Feb 21, 2015 at 01:22 AM 0
Share

the problem is that i'm using 3d object and unfortunatly it work better in spirit =( bad luck

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by toddisarockstar · Feb 20, 2015 at 06:49 AM

put it back how it was. the line you took out works fine. your getting an error cause your feeding raycast to much info. saying Camera.main.ScreenPointToRay (Input.mousePosition ) gives the raycast all the info it needs.

just say:

 if (Physics.Raycast(DirectionRay , Hit, Range)){}

without the "transform.position" part and you wont get errors. raycast format is only one point and one direction. so the first ray is a line from camera THROUGH mouse.
if you want a direction for your bullet, your variable "Hit" contains the location of the mouse. you can pull a vector three out of it by saying hit.point.

the "hit" varaible is what the mouse clicked on. not your bullet.

Comment
Add comment · Show 5 · 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 seb-lopez · Feb 21, 2015 at 01:20 AM 0
Share

well i'm on a good spot but i can't manage to fix the problems in the rigidbody part i get : "BCE0051: Operator '*' cannot be used with a left hand side of type 'UnityEngine.Ray' and a right hand side of type 'float'." it's in the line 13 (almost theire!!)

avatar image toddisarockstar · Feb 21, 2015 at 07:39 AM 0
Share

yeah, it wants a direction in vector three format. not raycast. its a different vaiable type. vector three can also represent direction if you simply subtract one vector three location from another. so we want to represent the objects position to where the mouse clicked. right? the raycast was from the camera down and i dont think you want things going down anyways.

so we i think this is the direction you want:

 var blastdirection:Vector3;
 
 blastdirection=Hit.point-tranform.position;
 rigidbody.AddForce (blastdirection * 10);

 
avatar image seb-lopez · Feb 21, 2015 at 11:44 AM 0
Share

wait does this meen that i have to take down the function raycast part and change all?

avatar image toddisarockstar · Feb 22, 2015 at 03:04 AM 1
Share

no, the rayscast part is good to find mouse position. but then you need to get direction from the character/bullet to the mouse click spot to represent wich way the "hit" object goes. I didnt test it but all together i think you would say it something like this:

 function shoot () {
    
     var Hit : RaycastHit; 
     
         var DirectionRay = transform.TransformDirection(Vector3.forward); // <== (the part i want to change)
  
          Debug.DrawRay (transform.position ,DirectionRay * Range , Color.blue);
         if (Physics.Raycast(transform.position , DirectionRay , Hit, Range)){
            
            
            if (Hit.rigidbody){ var blastdirection:Vector3;
  
  blastdirection=Hit.point-tranform.position;
  rigidbody.AddForce (blastdirection * 10);
         }
         }
  
avatar image seb-lopez · Feb 22, 2015 at 02:31 PM 0
Share

at line 13 i get unknown identifier : "transform"(point-tranform.position; is a little odd never seen it).

avatar image
0

Answer by seb-lopez · Feb 22, 2015 at 02:37 PM

hey i was thinking of a new way to make my dream shoot a reality. should i let the raycast script as before( streght line) and instead make the spawn point (empty game Object ) rotate towards the mouse would it be more easy or the post way is more easyer?

Comment
Add comment · Show 2 · 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 toddisarockstar · Feb 22, 2015 at 03:50 PM 0
Share

if it where my script i would simple move the object to the mousepoint.

   var hit:RaycastHit;
   var guythatgotshot:GameObject;//<--im sure you'll need it lol.
   var guythatisshooting:GameObject;//<--this is where bullet starts!
   var gobullet=false;
   var target:Vector3;
   
   guythatisshooting=GameObject.Find("name of shooter guy");
   
        function Update(){
        if(gobullet){    
        transform.position = Vector3.$$anonymous$$oveTowards(transform.position,target, 1);}
        
        
        
       if(Input.Get$$anonymous$$ouseButtonDown(0)){
       
             transform.position=guythatisshooting.transform.position;
             var Directionay = Camera.main.ScreenPointToRay (Input.mousePosition );
        
                     if (Physics.Raycast(DirectionRay , hit)){gobullet=true;
 guythatgotshot = hit.collider.gameObject;
 target=hit.point; } } }


honestly i dont think i would bother with addforce or anything like that unless you needed the object to bounce or something. this is a full script for what i think your talking about. make a new script with just this and stick it to your bullet object. i didnt test it but it should work. you shouldnt need to "spawn" a bullet. just put it in your scene with this script and it will just keep recycling itself.

avatar image seb-lopez · Feb 27, 2015 at 12:39 AM 0
Share

i will try ( sorry for late respond school homework came up)

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Push an object opposite direction of mouse position 0 Answers

What's wrong with my RayCastHit2D? 0 Answers

Instantiated an Prefab on a Spherical Object at mouse cursor position 3 Answers

How do I move only one selected object, rather than all objects with that script? 3 Answers

Raycasting not working as expected: rays are perpendicular to projectile's trajectory and collision doesn't occur 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