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 Blankstamped · May 28, 2014 at 03:30 AM · raycastcamera viewportlinecasthit.point

Need to find a point along a linecast slightly closer to the origin than hit.point

     RaycastHit hit;
     if (Physics.Linecast(head.transform.position, mainCam.transform.position, out hit)){
         mainCam.transform.position = hit.point;
     }

I'm trying to keep my camera from going through walls, and my solution for this was to have a linecast from the head of the character to the camera, and adjust the cameras location relative to the wall. I looked up GetPoint, but it doesn't apply to linecasts, and things get screwy when I use any other type of ray for this. I'm lost here.

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
1

Answer by Deign · May 28, 2014 at 04:16 AM

 Vector3 distanceVec = hit.point - mainCam.transform.position;
 mainCam.transform.position += distanceVec * percentOfDistanceToMove;

The last line can be replaced with

 mainCam.transform.position += distanceVec.normalized * (distanceVec.magnitude - stopDistance);

The first one moves you to a percentage distance closer to the position you're looking at. percentOfDistanceToMove = 1 puts you right on top of hit.point. percentOfDistanceToMove = 0.75 puts you at 75% the distance that the camera was at to the hit.point position. The second one moves you to a set distance from the hit.point. A value of 5 in stopDistance will place the camera 5 units away from hit.point

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 Blankstamped · May 28, 2014 at 12:55 PM 0
Share

The second line you supplied didn't work. The camera moved to a new position (off sides) and stayed there. I used the first one, which works, but I had to set percentOfDistanceTo$$anonymous$$ove to 1.5, because anything less than 1.0 set the camera farther back behind the object. I'm in class right now, so I'll get back to this later.

avatar image Deign · Jun 01, 2014 at 04:25 PM 0
Share

There's a typo, sorry about that. It should be += and not =. I have edited my answer to fix the typo :)

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

21 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

Related Questions

Linecast between character and camera doesn't ignoring Characters collider. 2 Answers

Linecast questions. 1 Answer

Moving gameobject along terrain based on player movement using raycast 1 Answer

Help with Raycast C# 0 Answers

Raycast.hitpoint always returning back to the camera 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