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 Ekta-Mehta-D · Mar 26, 2013 at 09:36 AM · collisionraycastbulletshoot

RayCast Problem : 2d

I am making space shooter kind of scene. But in that i am shooting in the direction of mouse position.

All this works fine. i am having Enemy script like this:

 function Start () {
     SetPositionandSpeed();
 }
 
 function Update () {
     var amttomove : float = currentSpeed * Time.deltaTime;
     transform.Translate(Vector3.down * amttomove);
     
     if(transform.position.y <= -6.00f)
     {
         SetPositionandSpeed();
     }
 }
 
 function SetPositionandSpeed()
 {
     currentSpeed = Random.Range(minSpeed , maxSpeed);
     x = Random.Range(-4.00f , 4.00f);
     z = -6.72f;
     y = 8.0f;
     transform.position = new Vector3(x , y , z);
 }

On mousebutton down I am Generating Bullet.

And my shooting script is this :

 function Update ()
 {
     var bullet : GameObject = GameObject.Find("Bullet");    
         var hit : RaycastHit ;
           var ray : Ray  = Camera .main .ScreenPointToRay (Input.mousePosition );
           if(Physics.Raycast (ray, hit)){     
              target = (hit.point - bullet.transform.position).normalized;
           }
             if(target != null){
             var moveAmount : float = 6 * Time.deltaTime;            
             transform.Translate(target * moveAmount);
          } 
          
           if(transform.position.y >= 7.0f)
           {
               Destroy(gameObject);
           } 
 }

But sometimes when my mouse position comes over the enemy , my bullet changes direction slightly.. So what do i need to do? May be Problem with RayCast..

Please Help me to solve out my problem..

Thanks for your support and help in advance..

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Apr 01, 2013 at 11:43 AM

Does "target" have an initial value like Vector3.up, because since there's only one Translation method it has to, otherwise the bullet won't move at all, right? In line 5 you do a raycast. Since there's not much to raycast at, everything from line 9 - 12 is executed only when you mouseover something. I bet that could be enemies or bullets. And since you do a calculation for a new direction Vector from bullet to enemy when pointing an enemy, of course it will move towards it.

The part where you instantiate the bullet is missing, but if this second script is attached to your player I would recommend, for object oriented purposes, move it to your Bulletprefab. Bullets should be able to move by themselves instead of being looked up every Update() from another script and pushed around.

Another question is: If you don't want the bullet to fly towards enemies when the mouse points on them, why do you have the raycast in there?

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

11 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

Related Questions

Bullet Effect 2 Answers

Bullet Shoot with RayCast don't destroy onCollisionEnter 1 Answer

Fire Bullet To Mouse Position Problem 2 Answers

Destroy Projectile on collision 3 Answers

Why won't my bullets detect a collision and be destroyed?... 4 Answers


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