Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Diego 2 · Feb 05, 2011 at 07:10 PM · cameracollisioncollisiondetectionspherecast

SphereCast issue on collision detection between camera and object

Hi!

I have to detect if any obstacle is present between the camera and the object it is following. I found a little script to achieve the detection and with the linecast it works perfectly, to gain more precision I want to use a spherecast but this doesn't work in the right way... what can I do? Here is the code attached to my camera, the camera is in the Object's hierarcy as its descendant.

var target : Transform;

// the height we want the camera to be above the target var height = 5.0; var heightDamping = 2.0; var rotationDamping = 3.0;

//camera collision variables var maxDistance = 5.0; var minDistance = 1.2; var smooth = 8.0;

private var dollyDir; private var distance;

// Place the script in the Camera-Control group in the component menu @script AddComponentMenu("Camera-Control/Smooth Follow")

function Awake() { dollyDir = transform.localPosition.normalized; distance = transform.localPosition.magnitude; }

function Update () { // Early out if we don't have a target if (!target) return;

//collision policy

//ideal camera position var desiredCameraPos:Vector3; var targetDirection:Vector3; desiredCameraPos= transform.parent.TransformPoint( dollyDir * maxDistance ); targetDirection = target.transform.position - transform.position; targetDirection = transform.InverseTransformDirection(targetDirection.normalized);

var hit:RaycastHit; var collflag = Physics.SphereCast( desiredCameraPos, 0.5, targetDirection, hit, maxDistance ); //Physics.Linecast( transform.parent.position, desiredCameraPos, hit );

Debug.Log("camera pos: "+transform.position +"target pos:"+target.transform.position); if( collflag ) {
var d; d = Mathf.Clamp( hit.distance, minDistance, maxDistance );
distance = Mathf.Lerp(distance,d,Time.deltaTime*smooth);
Debug.Log("camera collision:"+hit.collider.name + " distance:" +distance); } else { distance=Mathf.Lerp(distance,maxDistance,Time.deltaTime*smooth); }

//camera movement policy

// Calculate the current rotation angles wantedRotationAngle = target.eulerAngles.y; wantedHeight = target.position.y + height;

currentRotationAngle = transform.eulerAngles.y; currentHeight = transform.position.y;

// Damp the rotation around the y-axis currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

// Damp the height currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);

// Convert the angle into a rotation currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);

// Set the position of the camera on the x-z plane to: // distance meters behind the target transform.position = target.position; transform.position -= currentRotation Vector3.forward distance;

// Set the height of the camera transform.position.y = currentHeight;

thanks for helping!

D.

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
0

Answer by ceinicke · May 25, 2012 at 10:01 PM

Hi!

I haven't tested it but it looks like you are spherecasting from the desired position instead of the current position:

 var collflag = Physics.SphereCast( desiredCameraPos, 0.5, targetDirection, hit, maxDistance );

should be:

 var collflag = Physics.SphereCast( transform.parent.position, 0.5, targetDirection, hit, maxDistance );
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

1 Person is following this question.

avatar image

Related Questions

Physics.SphereCast please help understand 0 Answers

Understanding if an object is inside of another 1 Answer

How to get A Camera Collision? 1 Answer

Why are my bullets acting as being deflected by the camera borders collider? 0 Answers

Quick question about colliders 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