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 Borzi · Nov 30, 2013 at 03:53 PM · instantiateraycastcolliderbullet

Get an Object to Face/Move to RayCast Hit.point?

Hey! I was wondering how to get an object to move where the raycast ends or hits a collider. What I want to do is instantiate a bullet that always goes where the player shoots (so to check where that is, using RayCast). Anybody know how to do that? A script example would be much appreciated!

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 robertbu · Nov 30, 2013 at 04:12 PM 0
Share

Can you post your shooting code? The Raycast should be part of the shooting script. You would get the movement direction or end position from this raycast. Note if you have gravity turned on for the projectile, the position hit will not be the same as the raycast.

avatar image Borzi · Nov 30, 2013 at 05:28 PM 0
Share

I just want it to move in the direction of the ray cast hit and yes I am aware of that. I just use

 if(Physics.Raycast(FirePoint.position + Spread, FirePoint.forward, out Hit, Range))

so its pretty straightforward raycasting. Yes this was what I was intended on doing I was just not sure how.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by apomarinov1 · Nov 30, 2013 at 05:03 PM

This could be the bullet script:

 Vector3 endPoint = Vector3.zero;
 float      speed = 50f;
 
 void SetEndPoint(Vector3 p)
 {
     endPoint = p;
 }
 
 void Update()
 {
     if(endPoint != Vector3.zero)
         transform.position = Vector3.MoveTowards(transform.position, endPoint, Time.deltaTime * speed);
 }

Where you create the bullet you get a reference to it and send it the SetEndPoint function:

 Transform bullet = Instantiate(...);
 bullet.SendMessage("SetEndPoint", hit.point);

Furthure more if you are shooting at objects that might get destroyed, you could also send that object's reference to the bullet too, by adding a variable and a function for it:

 Transform enemy;
 
 void SetEnemy(Transform e)
 {
     enemy = e;
 }

Then you should add a direction variable for the enemy direction, so if the enemy is dead before the bullet hits him, it continues in the same direction:

 Vector3 enemyDirection;
 
 void Start()
 {
     enemyDirection = (enemy.position - transform.position).normalized;
 }

And you would add to the Update:

 void Update()
 {
     if(endPoint != Vector3.zero)
         transform.position = Vector3.MoveTowards(transform.position, endPoint, Time.deltaTime * speed);
     if(enemy)
         transform.position = Vector3.MoveTowards(transform.position, enemy.position, Time.deltaTime * speed);
     else
         transform.position += enemyDirection * Time.deltaTime * speed;
 }
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 Borzi · Nov 30, 2013 at 05:27 PM 0
Share

Interesting suggestions, thanks for your input! Although I have to say I do not like the idea of setting an "endpoint" for the bullet. Couldn't I just tell the bullet to keep moving forward in case there is no end point anymore (because an object or enemy/player moved out of the way)?

 if(endPoint == Vector3.zero)
 {
 transform.Translate(Vector3.forward * projectileSpeed * Time.deltaTime);
 }

Other then that I really like your solution to this thanks for your idea!

avatar image apomarinov1 · Nov 30, 2013 at 07:48 PM 0
Share

Well yeah, that is what I did with the enemy variable there. But this is more like a self pointing missle. If I got you right, firing the bullet in a direction will be sufficient, so you just instantiate it, set it's end point just so you can calculate the direction to that point. And then use the direction to move the bullet, if it hits something, cool, if the thing moves away the bullet continues. I dis that in a TD game demo, but there were many misses that the player would've thought were hits, so I made the bullets go to the enemies with the missle approach.

avatar image Borzi · Nov 30, 2013 at 07:56 PM 0
Share

Okidoki I will try it out :) Just another question though...wouldn't the bullet then go towards the enemy and not the actual hit point? :P

avatar image apomarinov1 · Dec 01, 2013 at 07:00 AM 0
Share

Yes that is correct. The hit.point is in world coordinates so even if the bullet goes toward that point, if the target moves the hit.point will not point to the first hit. I don't know if that would work, but to always move to a certain point on a moving object I guess you could try transfor$$anonymous$$g the hit.point in the local space of the target at the beginning and then when you need that hit point transform it in world coordinates that way you'll always get the new world position of the hit point to where the bullet would go.

avatar image Borzi · Dec 01, 2013 at 11:30 AM 0
Share

For me its not working right now but I believe this is because of the bullet itself. I'm going to have to do some more research. But thanks!

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

17 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

Related Questions

Raycast shooting problems 1 Answer

What would be a good way to not have objects spawn inside one another? 2 Answers

Particular collider not working properly... 1 Answer

Stop platforms spawning inside player 1 Answer

Should I use raycasting or colliders? 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