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 zeppeldep · Jul 16, 2014 at 07:20 PM · cameraraycastterraincollide

orbiting mouse controlled camera goes thru terrain

The plan is two make this available on PC only. Use keyboard to control the helicopters, use mouse to orbit view about helicopter. Problem: The camera is going thru the terrain, it will probably also go thru all meshes Video here shows current nasty... link text

Here is my scene showing the assets connected to the camera... alt text

Here is the code .. including my failed attempts to keep the camera "in the air"..

 using UnityEngine;
 using System.Collections;
 
 public class CamMouseOrbit : MonoBehaviour
 {
 
     public Transform target;
     public float distance = 10.0f;
     public float xSpeed = 5f;
     public float ySpeed = 2.5f;
     public float distSpeed = 10.0f;
     public float yMinLimit = -20.0f;
     public float yMaxLimit = 80.0f;
     public float distMinLimit = 5.0f;
     public float distMaxLimit = 50.0f;
     public float orbitDamping = 4.0f;
     public float distDamping = 4.0f;
     private float x = 0.0f;
     private float y = 0.0f;
     private float dist;
 
     void Start ()
     {
         dist = distance;
         
         Vector3 angles = transform.eulerAngles;
         x = angles.y;
         y = angles.x;
     
         // Make the rigid body not change rotation
     
         if (rigidbody)
             rigidbody.freezeRotation = true;
     }
     
     void LateUpdate ()
     {
         if (!target)
             return;
 
         Vector3 oPos = transform.position;
     
         x += Input.GetAxis ("Mouse X") * xSpeed;
         y -= Input.GetAxis ("Mouse Y") * ySpeed;
         distance -= Input.GetAxis ("Mouse ScrollWheel") * distSpeed;
     
         y = ClampAngle (y, yMinLimit, yMaxLimit);           
         distance = Mathf.Clamp (distance, distMinLimit, distMaxLimit);
     
         dist = Mathf.Lerp (dist, distance, distDamping * Time.deltaTime);        
         transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (y, x, 0), Time.deltaTime * orbitDamping);
 
         transform.position = transform.rotation * new Vector3 (0.0f, 0.0f, -dist) + target.position;
 
 //        Vector3 newPos = transform.rotation * new Vector3 (0.0f, 0.0f, -dist) + target.position;
 //        newPos.y = Mathf.Clamp(newPos.y,Terrain.activeTerrain.SampleHeight(oPos),0.5f);
 //        transform.position = newPos;
 
 
 //        float dist1 = newPos.y - Terrain.activeTerrain.SampleHeight(newPos);
 //
 //        if(dist1 < 0.5)
 //        {
 //            newPos.y = newPos.y + 0.7f;
 //        }
 //        transform.position = newPos;
 
 //        Vector3 wantedPosition = transform.rotation * new Vector3 (0.0f, 0.0f, -dist) + target.position;
 //
 //        //Vector3 bumperRayOffset;
 //        Vector3 back = target.transform.TransformDirection(-1 * Vector3.forward); 
 //        RaycastHit hit;
 //
 //        //if (Physics.Raycast(target.TransformPoint(bumperRayOffset), back, out hit, 2.5f)
 //        if (Physics.Raycast(target.position, back, out hit, 2.5f)
 //            && hit.transform != target) // ignore ray-casts that hit the user. DR
 //        {
 //            // clamp wanted position to hit position
 //            wantedPosition.x = hit.point.x;
 //            wantedPosition.z = hit.point.z;
 //            wantedPosition.y = Mathf.Lerp(hit.point.y + 1.0f, wantedPosition.y, Time.deltaTime * 5.0f);
 //        } 
 //        
 //        transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * 5.0f);
 
     }
 
     float ClampAngle (float a, float min, float max)
     {
         while (max < min)
             max += 360.0f;
         while (a > max)
             a -= 360.0f;
         while (a < min)
             a += 360.0f;
     
         if (a > max) {
             if (a - (max + min) * 0.5f < 180.0f)
                 return max;
             else
                 return min;
         } else
             return a;
     }
 
 }
 

How can I stop the camera from penetrating the terrain (and other meshes)? Thx ...

terrainthru.png (322.8 kB)
Comment
Add comment · Show 1
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 JaimeBGarcia · Mar 08, 2016 at 10:30 AM 0
Share

Hi mate, I have the same problem that you... I'm in Unity 5. Do you have the solution with any of these scripts? Can u share the final script please? I have wasted a week with the same problem that you have... Waiting for your answer, thank you :)

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by zeppeldep · Jul 18, 2014 at 07:14 PM

I have a temporary fix : )

add this function to make sure raycast between points hits only the Player...

     bool ViewingPosCheck (Vector3 checkPos)
     {
         RaycastHit hit;
         if(Physics.Raycast(checkPos, target.position - checkPos, out hit, distance))
             if(hit.transform.tag != "Player")
                 return false;
         newPos = checkPos;
         return true;
     }

then add it to the update like this...

         if(ViewingPosCheck(checkPos))
             transform.position = newPos;
         else
         {
             Vector3 v = target.position - checkPos;
             Vector3 tenPercent = v / 10;
             transform.position = Vector3.Lerp(transform.position, transform.position + tenPercent, Time.deltaTime);
         }

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 MaT227 · Jul 17, 2014 at 08:42 AM

There are several ways to avoid this problems but you'll need to use collision detection.

You'll need to add a collider on the camera and check if it is colliding with something. You can do this manually in code by handling all collision behaviours or you can add a collider material to make the camera slip on the surface.

There are a lot of tutorials that could be easily found about that topic. :)

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

Make the terrain ignore Raycast if in between Camera and Player? 1 Answer

Inserting UnityScript Raycasting code into C# script ? 3 Answers

Ray camera to Terrain goes Wrong. 1 Answer

RTS camera help 1 Answer

Raycasting on camera corners 3 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