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 Scribe · Nov 14, 2010 at 10:59 AM · raycastcamera-movementraycasthithover

Make camera hover at 50 over hilly terrain

Hi i am very new to unity and am currently concentrating on different camera scripts for an RTS type game. Currently when i press tab my camera zooms in and moves from a height of 200 to 50 and changes rotation. However at the height of 50 when i move the camera sometimes it goes into mountains and i was wondering if there was a way of making it hover at a constant height of 50 above the terrain. I started work on a script using Raycast (as seen below) but i don't no if I'm anywhere near the answer

var hit : RaycastHit;

function Update () {

if(Physics.Raycast (transform.position, -Vector3.up, hit)) {
    if(hit.distance < 50)
        transform.Translate(Vector3(0, 1, 0) * Time.deltaTime, Space.World);
}

}

Any help would be much appreciated, Thanks

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

2 Replies

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

Answer by matyicsapo · Nov 14, 2010 at 12:11 PM

var hit : RaycastHit;

var mTransform : Transform;

function Start () { mTransform = transform; / searching for the object's transform component every update frame especially multiple times is slow according to the [docs](3rd point)[1]/ }

function Update () { if(Physics.Raycast(mTransform.position, -Vector3.up, hit)) if (hit.transform.tag == "ground")) { mTransform.position.y = hit.point.y + expectedHeightFromGroundBasedOnZoom; // expectedHeightFromGroundBasedOnZoom would be 50 in your example } }

This way your camera will be exactly expectedHeightFromGroundBasedOnZoom distance units away from the ground on the Y axis.

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 Scribe · Nov 14, 2010 at 12:38 PM 0
Share

thanks for the answer but i put the script on my camera and it didn't seem to do anything. i changed expectedHeightFromGroundBasedOnZoom to 50 and tried changing "ground" to "Terrain" but still nothing

avatar image matyicsapo · Nov 14, 2010 at 12:58 PM 0
Share

put in a few Debug.Log's to see till where it works, like: does the Raycast hit anything at all? what's the tag of what it hits..i think that the problem is with one of the if's

avatar image Scribe · Nov 14, 2010 at 01:00 PM 0
Share

ok thanks i'll try that

avatar image
1

Answer by Vumi · Dec 31, 2012 at 12:44 PM

Use this, there were a few mistakes in that original code. With this script I have also included camera smoothing so that movement is not so jerky.

 #pragma strict
 
 var hit : RaycastHit;
 
 var mTransform : Transform;
 /*EDIT THESE SETTINGS*/
 private var cameraHoverHeight = 50;
 private var terrainName = "Terrain";
 /*------------------------*/
 
 /*DON'T EDIT THESE SETTINGS*/
 private var yVelocity = 0.0;
 /*------------------------*/
 function Start () {
     mTransform = transform; 
 }
 
 function Update () { 
 var smoothTime = 0.2/(camera.velocity.magnitude/100);
 
 
     if(Physics.Raycast(mTransform.position, -Vector3.up, hit))
        if (hit.transform.name == terrainName) {
        
        var newPosition : float = Mathf.SmoothDamp(transform.position.y,  hit.point.y + cameraHoverHeight,
                                  yVelocity, smoothTime);
 
 
    transform.position = Vector3(transform.position.x, newPosition, transform.position.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

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

2 People are following this question.

avatar image avatar image

Related Questions

How to detect UI Elements with Raycast 1 Answer

RaycastHit always returns 0 1 Answer

C# raycast shooting script broken 1 Answer

[RESOLVED]Raycast problem 2 Answers

Raycast collision issue 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