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 Orggrim · Mar 06, 2013 at 09:43 AM · physicsraycasttargetpointdrawray

Problem With Physics.Raycast

Hi, I am trying to create a Ray from my MainCamera to my Player

Here is the code I used...


 var target : Transform;
 
 function Update () 
 {
     
     Debug.DrawRay(transform.position, target.position);
     
     if (Physics.Raycast(transform.position, target.position))
      {
         Debug.Log("Something in the way");
      }
 }


Now I got it to work and all, but it doesnt seem to want to point towards the target! It points off at an angle, I need it to point directly down towards the Target.

Here are a few pics to show what I mean... alt text

Any help or suggestions would be greatly appreciated!!!

ask.jpg (49.9 kB)
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 Owen-Reynolds · Mar 06, 2013 at 04:46 PM

The second input(*) of a raycast isn't the place to look, it's the direction to look. In your case, should be target.position-transform.position

Is is a little confusing, since LookAt(player.position) does work -- the input is an actual map spot. In general, you always have to think "does it want an actual spot on the map, or a direction going to that spot?"

(*)Raycasts can either take two "start here" + "look this way" inputs, or a single Ray. But the two parts of the ray are "start here / look this way," so it's the same either way.

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 Orggrim · Mar 06, 2013 at 05:14 PM 0
Share

No luck, I got the same result either way, tried putting it like this

  if (Physics.Raycast(transform.position, target.position))
      {
         Debug.Log("Something in the way");
      }

and like this

  if (Physics.Raycast(target.position, transform.position,))
      {
         Debug.Log("Something in the way");
      }
avatar image Owen-Reynolds · Mar 06, 2013 at 11:47 PM 0
Share

The little dash wasn't a typo. It really was a $$anonymous$$us sign. It might help to look at a bunch of examples of other people using ray casts.

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

Raycast doesn't work properly? 0 Answers

Find a point at a distance from point on a line passing through two points 1 Answer

Physics.SphereCastAll with triggers 1 Answer

Raycasting to the bottom side of a plane? 2 Answers

Raycast hit point on face, not triangle 0 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