Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
This question was closed Feb 11, 2019 at 01:16 AM by cyberspacecat_ for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by cyberspacecat_ · Feb 10, 2019 at 07:06 PM · cameraraycastingthird-person-camerawall collision

Camera collision using raycasts (problems with SmoothDamp)

 void WallDetection()
 {
     float wallBuffer = 2f;

     if (Physics.Raycast(TargetLookAt.position, -transform.TransformDirection(Vector3.forward), out hitInfo, Distance, walls))
     {
         Distance = hitInfo.distance - wallBuffer;
     }
 }

I have a functional Raycast solution in my camera code that tracks wall collisions and adjusts the camera relative to the player, except the camera code I have is built off code that has built in smoothing into the distance calculation, and it's making my raycast "bounce" off colliders and messing up the raycast check.


 void CalculateDesiredPosition()
 {
     // Evaluate distance
     Distance = Mathf.SmoothDamp(Distance, desiredDistance, ref velDistance, DistanceSmooth);
 
     // Calculate desired position
     desiredPosition = CalculatePosition(mouseY, mouseX, Distance);
 }

 Vector3 CalculatePosition(float rotationX, float rotationY, float distance)
 {
     Vector3 direction = new Vector3(0, 0, -distance);
     Quaternion rotation = Quaternion.Euler(rotationX, rotationY, 0f);
     return TargetLookAt.position + rotation * direction;
 }

 void UpdatePosition()
 {
 
     var posX = Mathf.SmoothDamp(position.x, desiredPosition.x, ref velX, X_Smooth);
     var posY = Mathf.SmoothDamp(position.y, desiredPosition.y, ref velY, Y_Smooth);
     var posZ = Mathf.SmoothDamp(position.z, desiredPosition.z, ref velZ, X_Smooth);
     position = new Vector3(posX, posY, posZ);
 
     transform.position = position;
 
     transform.LookAt(TargetLookAt);
 }

The issue in question - I'm a novice coder so I'm probably missing something obvious. I'd like it to either make it perform the same task without the smoothing issue, or preferably modify the Raycast code so that it takes the smoothing in mind when calculating collisions.


Any help is appreciated

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

  • Sort: 
avatar image
0

Answer by cyberspacecat_ · Feb 11, 2019 at 01:15 AM

After some trial and error I managed to make it functional for my own purposes. - I had overthought this problem severely and could, instead of using a SmoothDamp, smooth the distance with a simple Lerp.


     void CalculateDesiredPosition()
     {
         // Evaluate distance
         Distance = Mathf.Lerp(Distance, desiredDistance, DistanceSmooth * Time.deltaTime);
 
         // Calculate desired position
         desiredPosition = CalculatePosition(mouseY, mouseX, Distance);
     }

And modified my Raycast code slightly, as I found no wall clipping issue using a Lerp and the extra unit made the camera spin wildly as I got close to the wall.


     void WallDetection()
     {    
         if (Physics.Raycast(TargetLookAt.position, -transform.TransformDirection(Vector3.forward), out hitInfo, desiredDistance, walls))
         {
             Distance = hitInfo.distance;
         }
         else
         {
             Distance = desiredDistance;
         }
     }
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

Follow this Question

Answers Answers and Comments

160 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

Related Questions

Camera Rotates Incorrectly 2 Answers

Moving FPS/TPS camera to look at a target and resuming player control 1 Answer

transform.forward doesn't correspond to rotation 1 Answer

how can i instantiate a GameObject in mouse position and in a special distance from the camera? 2 Answers

How do you determine if an object is visible to the camera? 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