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
4
Question by thenachotech1113 · Dec 05, 2013 at 11:11 PM · raycastvector3lightdirection

direct a raycast from point a to point b

hello everyone, i am attempting to make a space shooter game, and thought of adding solar pannels to the spaceships, that way people cant hide in a behind an object forever to prevent death. so the best way i could think of doing this is by using a colider on the light to detect when an object is in range to actualy be able to charge from the light and a raycast to be casted from the light to the spaceship`s solarpanel position do detect if there is something in the way. so my question is how do i input the direction to the raycast if i only have 2 positions?

thanks for the help everyone.

p.s. i did not include code simply because i did not think it would be nessesary to answer the question, but if its needed pls tell me and i will repost the question with code

Comment
Add comment · Show 1
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 Mudman2999 · Dec 05, 2013 at 11:13 PM 0
Share

when you said only two positions did you mean the game is 2d?

2 Replies

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

Answer by steakpinball · Dec 05, 2013 at 11:28 PM

To get a vector from one position to another simply subtract the source from the destination.

 Vector3 fromPosition = source.transform.position;
 Vector3 toPosition = destination.transform.position;
 Vector3 direction = toPosition - fromPosition;

The same principle applies to any type of vector.

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
avatar image
21

Answer by Eric5h5 · Dec 05, 2013 at 11:41 PM

Use Linecast instead of Raycast.

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 Rocketman123 · Apr 26, 2017 at 10:50 PM 0
Share

Would that shoot out from the current object's position to the target object?

avatar image sandeepsmartest Rocketman123 · Apr 27, 2017 at 04:56 AM 1
Share

Try this - It is just a pseudo code(Untested)

  RaycastHit hit;
             Vector3 fromPosition = source.transform.position;
             Vector3 toPosition = destination.transform.position;
             Vector3 direction = toPosition - fromPosition;
         
     
             if(Physics.Raycast(source.transform.position,direction,out hit))
                 {
                     print("ray just hit the gameobject: "+hit.collider.gameObject.name);
                 }

   

hope this may help you. Nsks

avatar image Eric5h5 sandeepsmartest · Apr 27, 2017 at 05:08 AM 1
Share

Or how about just using Linecast instead of Raycast? No need to make things complicated.

Show more comments

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

23 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 avatar image

Related Questions

Vector direction for a reticle 1 Answer

Raycast between TransformDirection and Vector3.Forward? 1 Answer

Generate a random direction within min and max angles 2 Answers

C# Raycast from Object direction (z-axis) + another Vector3 1 Answer

Declaring Vector in inspector for directional movement 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