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 BluesyPompanno · Aug 28, 2017 at 06:07 PM · cameracamera-movement3rd person controllercamera rotation3rd person camera

How to stop 3rd person camera going throught walls ?

Hello I am strugling with a 3rd person camera controll.

I want the camera to get closer to the player when it hits the wall or any obstacle.

But i don't know how I've tried putting colliders onto it but it doesn't work.

Does anybody knows how to fix this ?

This is my code ->

public float mouseSensitivity = 10; public Transform target; public float distanceFromTarget = 2; public Vector2 MinMaxViewAngle = new Vector2( -40,85);

 public float rotationSmoothTime = .12f;
 Vector3 rotationSmoothVelocity;
 Vector3 currentRotation;

 float Xrotation;
 float Yrotation;

 // Update is called once per frame

 private void Start()
 {
     Cursor.lockState = CursorLockMode.Locked;
     Cursor.visible = false;
 }
 void LateUpdate ()
 {
     Xrotation += Input.GetAxis("Mouse X") * mouseSensitivity;
     Yrotation -= Input.GetAxis("Mouse Y") * mouseSensitivity;
     Yrotation = Mathf.Clamp(Yrotation, MinMaxViewAngle.x, MinMaxViewAngle.y);

     currentRotation = Vector3.SmoothDamp(currentRotation, new Vector3(Yrotation, Xrotation), ref rotationSmoothVelocity, rotationSmoothTime);
     transform.eulerAngles = currentRotation;

     transform.position = target.position - transform.forward * distanceFromTarget;
 }

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
2
Best Answer

Answer by JonPQ · Aug 28, 2017 at 07:25 PM

1) remove the colliders. 2) position/ move your lead game object.... e.g. car or character. 3) generate a vector backwards from your character, (maybe using combination of character's facing, and up vectors) in the direction you want the camera to be, and also generate the point along that vector at the distance you want the camera to be from the object... say 10 unity units. 4) Then use Physics.raycast( out hit) Which outputs collision data... if there is no collision... set your camera at that point... and set its transfor.forward to point at your character (the other direction along the vector) 5) If there is a collision (or N collisions) it will give you a list of collision points in the 'out' Choose the closest one, to position your camera there.

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 JonPQ · Aug 29, 2017 at 03:30 PM 1
Share

one extra tip, use Physics.spherecast ins$$anonymous$$d of rayscast... OR subtract a bit of extra distance from the collision point back along the collision vector... This will keep the camera away from the wall a bit.

avatar image BluesyPompanno JonPQ · Sep 06, 2017 at 10:20 AM 0
Share

Iˇve managed to do it other way. I´ve created a gameobject that holds the camera and did a raycast in all of its directions and when it hits something move the way out of the wall (push it from the wall) then i was able to update cameras location and when it stopped hitting the wall , move it back to its original position and sent a raycast towards the player that warns that it returned to its original position.

Thanks for the advice,You helped alot

avatar image JonPQ BluesyPompanno · Sep 06, 2017 at 04:14 PM 0
Share

Glad to be of assistance :) a couple more tips... the raycast has a layer paramater... Raycasting is slow... so you can make simple collision layer with boxes... ins$$anonymous$$d of complex geometry.... put them in a (non-rendered) layer (cameras use the layers also) then do the raycast only to that layer.... That will get you a big speed up on the raycasting.

The other simple tip, is try not to use too many ray casts.... it can be slow ! at least try to limit it to one ray or sphere cast... until it looks like there is a collision you cant resolve easily... then add in the extra 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

108 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

Related Questions

How to move camera position to player object's direction while having looking at object? 0 Answers

Getting the camera to move in the direction its facing. 0 Answers

Returning the camera back to original position, 1 Answer

How can I mimic this game's camera in Unity? 1 Answer

Rotate 3rd person camera around player 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