Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Ricky_Hammett · Dec 31, 2017 at 09:07 PM · unity 5camerascript.wall collision

How to validate camera when collides with a wall - ThirdPerson

Hi guys I make a videogame with a player in thirdperson , the camera movement depend of player , I have code (c#) for camera. This code detect if collides with a wall , I have the validation but I do not know what to do when the if or the else be fulfilled , some idea ? Thanks guys!

this is the code : if (Physics.Raycast (transform.position, backWard, distanceAway)) { // do somethings } else { // do somethings }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Tpizzle13 · Dec 31, 2017 at 09:11 PM

@Ricky_Hammett

What do you mean by validate camera?

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 Ricky_Hammett · Dec 31, 2017 at 09:27 PM 0
Share

validate if camera collides with a wall ;)

avatar image Tpizzle13 Ricky_Hammett · Dec 31, 2017 at 09:39 PM 0
Share

what exactly are you trying to do? validate the camera to do what? Also I don't understand what you mean by "validate".

avatar image Ricky_Hammett Tpizzle13 · Dec 31, 2017 at 09:47 PM 0
Share

I need to know what action I can put inside the if and within the else in the code that I put in the post, that code validates if the camera is collisioning with a wall, when this is done I do not know what to do. The idea is that the camera does not penetrate the walls.

Show more comments
avatar image
0

Answer by Lysander · Dec 31, 2017 at 10:42 PM

A couple of different ways to accomplish this, but none of them are perfect. Solution 1 is, as Tpizzle13 says, fire raycasts from the camera to the player each frame, and move forward when something is hit that keeps it from reaching the player. You can't really account for the thickness of an object, so you'll likely need to keep firing raycasts / moving forward until you can hit the player with the raycast again, and then you're fine. You can smooth this out by setting camera destination targets and lerping between them, so it's not jumpy / jittery and the camera just smoothly tries to keep the player in view.

It's worth noting that, for raycasts, you should be really specific about the layers of objects that count as "obstacles", so you aren't constantly moving forward in response to hitting special effects or small desks / whatever that only partially block the view and aren't "walls".

The second option is to use a collider on the camera, and the rest is more or less how the raycast method works, but inside of OnCollisionEnter/OnCollisionStays instead. Just keep moving the camera forward until it's no longer colliding, and you should be more or less okay- use camera destination targets and lerp in order to smooth things out.

If you want to be fancy and do a bit of studying, you can also use the Cinemachine system in the UAS. It already has functionality built-in to do exactly this, but it's a complex system and takes some time to get used to. There are likely a thousand other assets both on the UAS and shared freely on the web that can also fulfill the specific need you have here, but Cinemachine is a top-tier, phenomenal extension, and free, so I'd say give some tutorials a whirl for that.

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

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

180 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 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 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 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 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 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 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 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

Google cardboard - Wrong cameras? 0 Answers

How to end a cinematic camera cycle 1 Answer

My Camera Do Not Limit Its Angles And It Glitches, How I Can Solution It? 0 Answers

Can you show a Example of script using the animator override controller?to substitution the original animation clip 1 Answer

initiate Imagebuttons in loop 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