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
0
Question by Flipbee9 · Jun 18, 2010 at 12:06 AM · cameracollisionthird-personcs0103

3rd Person Camera Collision

I've been working with Unity for some while to try to create basic games. Lately, I've been running into a problem. I'm using the script in the 3D Platform Game for my camera and character. I like how it works, but lately, I've been running into a problem with the camera. It's great, but when I run into a hill in the terrain, you can see through it which kinda ruins the effect. I've seen ray casting done on this stuff, but I"m a n00b at this, and the scripts other's supply don't really get that effect I want. Thanks in advance if you can help :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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Tetrad · Jun 18, 2010 at 01:41 AM

This is untested, but should get the point across. I've ripped apart a script that we're using in the game here but with the mouse wheel (or -/= keys) move in/out stuff taken out.

Just make a new class (C#) and drop this in there and attach it to your camera object.

For this script I assume that the hierarchy is something like player base (ground) -> player shoulder-ish -> camera (placed away from the shoulders kind of like a dolly).

public float minDistance = 1.0f; public float maxDistance = 10.0f;

Vector3 dollyDir; float distance;

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

void Update() { Vector3 desiredCameraPos = transform.parent.TransformPoint( dollyDir * distance );

     // (optional) put layers you don't want to collide with  here (probably things like enemies)
     const int ignoreLayer1 = 1 << 18; 
     const int ignoreLayer2 = 1 << 19;
     int layerMask = ignoreLayer1 | ignorelayer2;
     layerMask = ~layerMask; 

     RaycastHit hit;
     if( Physics.Linecast( transform.parent.position, desiredCameraPos, out hit, layerMask ) )
     {
         distance = Mathf.Clamp( hit.distance, minDistance, maxDistance );
     }

     transform.localPosition = dollyDir * distance;

}

Comment
Add comment · Show 5 · 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 Flipbee9 · Jun 18, 2010 at 02:43 AM 0
Share

Well, I'm not so smart when it comes to C# so can you be a bit more specific on how I should do this. I'm not used to C# scripting

avatar image Tetrad · Jun 18, 2010 at 04:07 AM 0
Share

Just put that code in a class and attach that script to whatever holds your camera. That's assu$$anonymous$$g that the camera is a child of your player in the hierarchy.

avatar image Flipbee9 · Jun 18, 2010 at 04:09 AM 0
Share

When I apply the script it says

"Assets/ThirdPersonCamera.cs(24,40): error CS0103: The name `ignorelayer2' does not exist in the current context"

avatar image Tetrad · Jun 18, 2010 at 04:48 AM 0
Share

It's a typo, and meaning to be an example. Just remove that layer stuff.

avatar image Flipbee9 · Jun 18, 2010 at 12:26 PM 0
Share

For some reason, it just won't work. The scripts I'm using are the ones in the 3D Platformer Tutorial from Unity3d.Com

avatar image
1

Answer by increpare · Jul 25, 2010 at 12:22 PM

Here's a slightly tidied-up/tested version of Tetrad's C# script - it zooms smoothly back out after you get too close to a wall (the other one stays close to your character after going towards a wall and back). Just need to attach it to a camera (that's parented to the object being moved).

using UnityEngine; using System.Collections;

public class CameraCollision : MonoBehaviour {

 public float minDistance = 1.0f;
 public float maxDistance = 4.0f;
 public float smooth = 10.0f;

 Vector3 dollyDir;
 float distance; 

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

 void Update()
 {
         Vector3 desiredCameraPos = transform.parent.TransformPoint( dollyDir * maxDistance );

         RaycastHit hit;
         if( Physics.Linecast( transform.parent.position, desiredCameraPos, out hit ) )
         {
             distance = Mathf.Clamp( hit.distance, minDistance, maxDistance );
         }
         else
         {
             distance=maxDistance;
         }

         transform.localPosition=Vector3.Lerp(transform.localPosition, dollyDir * distance, Time.deltaTime * smooth); 
 }

}

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 capzulu · Aug 17, 2011 at 04:51 PM 0
Share

Hey, i'm combining this script with the $$anonymous$$ouseOrbit script but the colisions aren't working....

avatar image SalvoSoftware · Apr 25, 2013 at 12:05 PM 0
Share

$$anonymous$$ee too, maybe you should edit mouseorbit script to make it work

avatar image Kissanaattori · Feb 11, 2014 at 06:55 PM 0
Share

These should be separate scripts, this one's C# and the default $$anonymous$$ouseOrbit is Javascript..

avatar image
1

Answer by Code_Monkey_Refurbished · Jan 12, 2015 at 06:54 AM

or...

     private void compensateForWalls(Vector3 fromObject, ref Vector3 toTarget)
     {
         Debug.DrawLine (fromObject, toTarget, Color.cyan);
 
         RaycastHit wallHit = new RaycastHit ();
         if(Physics.Linecast(fromObject, toTarget, out wallHit))
         {
             Debug.DrawRay(wallHit.point, wallHit.normal, Color.red);
 
             Vector3 wallHitVector3 = new Vector3 (wallHit.point.x, wallHit.point.y, wallHit.point.z);
 
             toTarget = new Vector3 (wallHitVector3.x, toTarget.y, wallHitVector3.z);        
         }
     }
 
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
avatar image
0

Answer by angad singh · Feb 24, 2011 at 12:12 PM

hereyou go, a prefab :)

http://blockmonkey.com/my-work/unity-3d-work/

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Jittery Collision Detection 0 Answers

3rd person camera that avoids objects 0 Answers

How to keep the player on the left side o the screen? 2 Answers

Prevent camera from going trough objects? 0 Answers

3rd person camera through wall 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