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 awts202 · Nov 24, 2017 at 08:09 AM · camerawall collision

Camera goes through walls when moving fast.

Hello, I have a dynamic camera that follows it's target but passes through walls when I move it very fast. My script works but It isn't working I supposed to. Where is my problem exactly? Any help is really appreciated.

Here is my script.

 public GameObject target;
 public float distance;
 public float xSpeed = 2.0f;
 public float ySpeed = 2.0f;
 public float yMinLimit = -90f;
 public float yMaxLimit = 90f;
 public float distanceMin = 15f;
 public float distanceMax = 10f;
 public float smoothTime = 5f;
 public float camCollisionOffset = 1;
 float rotationYAxis = 0.0f;
 float rotationXAxis = 0.0f;
 float velocityX = 0.0f;
 float velocityY = 0.0f;
 public LayerMask CamOcclusion;

 private Vector3 offset;
 // Use this for initialization
 void Start()
 {
     Vector3 angles = transform.eulerAngles;
     rotationYAxis = angles.y;
     rotationXAxis = angles.x;
     offset = transform.position - target.transform.position;
     if (GetComponent<Rigidbody>())
     {
         GetComponent<Rigidbody>().freezeRotation = true;
     }
 }

 void FixedUpdate()
 {
 }

 void LateUpdate()
 {
     if (target)
     {       
         if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
         {
             velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
             velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
         }
     
         rotationYAxis += velocityX;
         rotationXAxis -= velocityY;
         rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
         Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
         Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
         Quaternion rotation = toRotation;

         float nowDistance = distanceMin;
         Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
         Vector3 position = rotation * negDistance + target.transform.position;
         transform.rotation = rotation;
         transform.position = position;
             Ray ray = new Ray(transform.position, -transform.forward);
             RaycastHit hit;
             if (Physics.Raycast(ray,out hit,distanceMin+camCollisionOffset, CamOcclusion))
             {
                 nowDistance = hit.distance - camCollisionOffset;    
             }
         distance = Mathf.Lerp(distance,nowDistance,50* Time.deltaTime);
         velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
         velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
     }

 }
 public static float ClampAngle(float angle, float min, float max)
 {
     if (angle < -360F)
         angle += 360F;
     if (angle > 360F)
         angle -= 360F;
     return Mathf.Clamp(angle, min, max);
 }
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 Dray · Nov 24, 2017 at 10:20 AM

Use Rigidbody.MovePosition for moving your camera. If you do so, physics will be taken into account.

What you are doing right now, since you are setting the transforms position by hand, is "teleporting" the camera over a tiny distance each frame, ignoring all colliders and forces around it.

Also make sure the camera has a collider and move the part where you are changing the actual position into FixedUpdate for higher accuracy.


EDIT to integrate the comments, try adding this to your component

 public Vector3 cameraMovement;
 public float cameraRadius = .5f;

 void FixedUpdate()
     {
         var frameMovement = cameraMovement * Time.fixedDeltaTime;
 
         RaycastHit hit;
         if (Physics.SphereCast(camera.transform.position, cameraRadius, frameMovement, out hit, frameMovement.magnitude))
         {
             camera.transform.position = hit.point + hit.normal * cameraRadius;
         } else {
             camera.transform.position += frameMovement;
         }
     }

Let me know if this works

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 awts202 · Nov 24, 2017 at 10:32 PM 0
Share

As much as possible I don't want to use Rigidbody on my camera. Is there any way to do this?

avatar image Cherno awts202 · Nov 24, 2017 at 11:08 PM 0
Share

Save the camera's current position. In the next frame, cast a ray from the now-current position to the saved last-current position. If it hits anything, move the camera to the hit.point of the ray.

avatar image Dray awts202 · Nov 26, 2017 at 07:49 PM 0
Share

Right that will work too, you can even use Physics.SphereCast to give your camera some kind of radius

avatar image
0

Answer by awts202 · Dec 03, 2017 at 07:27 PM

I found the problem, my camera only moves when the camera hits something but won't move when it doesn't hit anything. I should make my camera move closer to my target when something block camera's view. How to accomplish this? Pls help. Thnks in advance.

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

112 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

Related Questions

Camera collision using raycasts (problems with SmoothDamp) 1 Answer

How to validate camera when collides with a wall - ThirdPerson 2 Answers

How to make camera position relative to a specific target. 1 Answer

Accessing all GameObjects with a certain tag 1 Answer

nullifying targetObject after a Smooth LookAt Transition 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