Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 HHameline · Jun 17, 2011 at 02:08 PM · collisionterrainmovingcollide

Camera Colliding with Terrain

Hello, I want to make my camera in my racing game collide with the terrain so it cant go through the terrain at all. This is the code I added to my script in order to try and achieve the behaviour I'm looking for:

 using UnityEngine;
 using System.Collections;

 public class SmoothFollow : MonoBehaviour 
 {
     private Ray ray;
     private RaycastHit hit;
     public GameObject player;
     public Transform target;

     void Start()
     {
         ray = new Ray(target.position, target.position - transform.position);
     }

     void Update()
     {
         if (Physics.Raycast(ray, out hit))
         {
              transform.position = ray.GetPoint(hit.distance - 1);
         }
 
         if(cameraNum == -1)
         {
              cameraDistance = Vector3.Distance(target.position, target.position) + 140;
         }
         // Gets the sled position and adds the camera height to it.
         newHeight.y = target.transform.position.y + cameraHeight;
     
         if(Input.GetKeyUp(KeyCode.C) && allowCameraChange)
         {
             ChangeCameraView();        
         } 
     }
 }

Its not giving me any errors its just not doing anything at all besides the normal camera stuff I have going on in the script. Any help or insight would be awesome.

Thanks, Hans

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 Jerdak · Jun 18, 2011 at 05:03 PM 0
Share

Can I ask maybe a simple question, does your object not move? Your ray is only set once at the start of the level.

A simpler approach I took for terrain camera collision was to do a simple height test at the camera's current x/z position and use that as the camera's y. This is only useful if you only care about terrain collisions.

1 Reply

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

Answer by testure · Jun 18, 2011 at 06:12 PM

Your ray does not change, so nothing is going to happen. You need to be doing the raycast every frame, or at some regular interval for occlusion/collision testing to 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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Terrain collision and specific collision problems . 1 Answer

Making the object collide while moving in the forward dir. (Vector3.forward) 1 Answer

Collision Help 1 Answer

How can I make a sound deploy after colliding with a certain object (or terrain)? 1 Answer

How to let the player collide with animated objects? 0 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