• 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
Question by Vince · Jun 09, 2010 at 05:13 PM · cameracollisiongameobject

Camera Collison with scene objects

I think this is more of a 3D math question over programming.

I have a Kinematic Ridgidbody Main camera with a .03m sphere collider that is free roaming, looking around various large scene objects while mesh colliders. Currently i can see collisions, as all my OnCollisionEnter/Stay/Exit all throw debug logs. However i want to keep the camera from passing through the object and even bounce back a bit on the angle of reflection from the camera's angle of movement and the collision point's normal reality to the collision point. (I might have to settle with the angle of incidence from the camera itself if mesh object's geometry is too complex.

As you can see with the code below I've been trying different things, and at low speeds, the camera bounces off the object at various angles given the mesh collider's state. I want to prevent pass-through of the object by the camera indefinitely.

void OnCollisionEnter(Collision info) { Debug.Log("Collision!"); cameraRigidbody.isKinematic = false;

 Vector3 norm;
 norm = Vector3.Cross(info.impactForceSum.normalized);
 cameraRigidbody.AddRelativeForce(norm);


} void OnCollisionStay(Collision info){ Debug.Log("Colliding!"); //cameraSpeed = 0; Vector3 norm; norm = Vector3.Cross((info.contacts[0]).point, transform.position); //cameraRigidbody.AddRelativeForce(norm); cameraRigidbody.AddRelativeForce(info.impactForceSum.normalized); info.contacts[ //int bounceDistance = 1; //Vector3 bouncedPosition = transform.position + (bounceDistance * norm); //flyToPosition = bouncedPosition; Debug.Log("Still Colliding!"); }

void OnCollisionExit(Collision info){ Debug.Log("Done Colliding!"); //cameraSpeed = settings.CameraSpeed / 500f; }

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Ehren · Jun 09, 2010 at 05:39 PM

The reason the camera can pass through other objects is that it's kinematic. You could try changing it to a non-kinematic rigidbody and moving it by applying forces to it, but that could be a bit tricky to get right.

A simpler option would be to keep the camera kinematic and use a combination of raycasting and Physics.CheckSphere. Before moving the camera, raycast to the point you want to move to and make sure you won't be colliding with anything. You can also use Physics.CheckSphere to determine if there is enough space at the destination for your camera's collider sphere to reside without touching other colliders.

Comment
Noise crime
Vince

People who like this

2 Show 2 · 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 Vince · Jun 09, 2010 at 05:47 PM 0
Share

I tried to temporarly make the camera non kinematic, but i guess once its moving that wont really help. For some reason, the terrain works fine. Is that a special property of the floor that Unity handles or am I missing something?

avatar image Ehren · Jun 10, 2010 at 02:54 AM 0
Share

Is the camera being controlled by the player? Or is it following something? There might be a premade script you could use (such as the First Person Controller http://unity3d.com/support/documentation/Manual/HOWTO-First%20Person%20Walkthrough.html).

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How to stop Camera from going into colliders 3 Answers

Smooth Camera 2 Answers

object collision attatched to camera 1 Answer

Rotate camera around gameobject 1 Answer

Detecting if object in array collides with another object in same array 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