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
1
Question by ben0bi · May 22, 2012 at 09:21 AM · cameraraycastdistancewallcollide

Ray Cast not working for camera.

Hello,

I am trying to make a 3rd person camera script. But it does not work and I don't know what I am doing wrong.

First the explanation: There is a far-cam-point, this is the max distance between player and camera. The close cam point is the same with z=0. If the camera hits a wall, I just need to apply the (negative) distance between the closecampoint and the hit of the raycast to z of farcampoint and can then set the cam position to this new farcampoint.

I draw Debug-Lines all over here: Cyan is farcampoint to closecampoint. Yellow is closecampoint to hit and red is the raycast itself. All lines are aligned over each other, so that is fine. But it doesnt seem to hit anything when I approach a wall. Any help with this would be great.

(Ah, yes...lerp and slerp will come later... ;) )

Here is the code: //[new] Vector3 farCamPoint = player.position + camYRotation pivotOffset+ aimRotation camOffset; Vector3 closeCamOffset=new Vector3(camOffset.x, camOffset.y, 0.0f); Vector3 closeCamPoint= player.position+camYRotation*pivotOffset+aimRotation*closeCamOffset;

     Debug.DrawLine(farCamPoint, closeCamPoint, Color.cyan);
     
     RaycastHit hit;
     Vector3 raycastDir = farCamPoint - closeCamPoint;
     float distance=Vector3.Distance (farCamPoint, closeCamPoint);
     //raycastDir.Normalize();
     float padding = 0.3f;
     if (Physics.Raycast(closeCamPoint, raycastDir, out hit, distance, mask)) {
         farCamPoint=new Vector3(camOffset.x, camOffset.y, - Vector3.Distance (closeCamPoint, hit.point));
         Debug.Log ("Hit the wall");
     }
     
     cam.position=farCamPoint;
     Debug.DrawLine(farCamPoint, closeCamPoint, Color.yellow);

     Debug.DrawRay (closeCamPoint, raycastDir,Color.red);
Comment
Add comment · Show 4
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 DaveA · May 22, 2012 at 08:22 PM 0
Share

Do the walls have colliders on them? And are they in layers which are not excluded from raycasting?

avatar image dannyskim · May 22, 2012 at 08:34 PM 0
Share

Also, how are you declaring your layer$$anonymous$$ask? Is it a public variable? Did you construct a bitwise statement yourself?

avatar image Swift_On_Fire · May 22, 2012 at 08:57 PM 0
Share

How is it behaving currently?

avatar image ben0bi · May 22, 2012 at 09:58 PM 0
Share

There is a Transform called "player" where the camera turns around.

This is how the mask is created, I think it should only exclude the player and nothing else - not?

And yes, the walls have colliders. I tried it with box-collided walls and with a mesh collider for a whole house, nothing works.

Here is the mask code:

 mask = 1 << player.gameObject.layer;
 // Add Ignore Raycast layer to mask
 mask |= 1 << Layer$$anonymous$$ask.NameToLayer("Ignore Raycast");
 // Invert mask
 mask = ~mask;

 cam = transform;

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ben0bi · May 23, 2012 at 10:31 AM

[Solved]

It was the mask as you mentioned. I just copied it from the camera script in the 3rdPersonShooter example where it is also used for aiming at stuff. (Thats done in another script by me..)

so, set the mask to everything and it works.

 mask=1<<LayerToMask.NameToLayer("Everything");
 mask=~mask;

and it works... (not really tested yet but it already moves the camera...anywhere...hope its the right spot..i got another ray just now, hope it works with the ray above. ;) )

Comment
Add comment · Show 3 · 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 ben0bi · May 23, 2012 at 10:42 AM 0
Share

Yay it works. You're free to use it yourself ;)

avatar image ben0bi · May 23, 2012 at 10:43 AM 0
Share

The other vars are: camOffset: the maximum camera offset from pivotoffset...and pivotOffset: the offset from the player-Transform.

avatar image dannyskim · May 23, 2012 at 07:01 PM 0
Share

For future use case, you may want to consider making the mask variable declaration public. If you do so, you can view the mask in the inspector, and it will have a drop down check list like it does on any Camera where you can set the layers there. I find this method a lot easier than having to deal with bitwise statements.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

RTS camera help 1 Answer

Jarring "snap" on 2d Camera raycast collider 0 Answers

orbiting mouse controlled camera goes thru terrain 3 Answers

Prevent camera from going through walls and meshes 1 Answer

Camera raycast? 2 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