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 Jason Hamilton · Sep 23, 2010 at 11:10 AM · camerajavascriptterrain

Camera not through terrain

hey, how do I stop my camera, which is following a player by a smooth follow script, not go through the terrain?

JAVA please.

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 Jessy · Oct 11, 2010 at 01:45 AM 2
Share

http://www.dannyg.com/ref/javavsjavascript.html

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by spinaljack · Sep 23, 2010 at 02:45 PM

You can use a ray cast from the target position towards the camera, if the raycast hits terrain or building then you move the camera forwards to the point of the raycast hit. If you want to ignore certain things like the player you can set them to the ignore raycasts layer or you can use physics raycast all and just loop through all the hits ignoring certain objects (like the player)

e.g.

var distance : float; // follow distance var distanceOffset : float; // how much to reduce the follow distance by to avoid objects var target : Transform; // where the camera is looking at

function Update(){ var relativePos = transform.position - target.position; var hit : RaycastHit; if (Physics.Raycast (target.position, relativePos, hit, distance+0.5)) { Debug.DrawLine(target.position+Vector3.up,hit.point); distanceOffset = distance - hit.distance + 0.8; // add near clipping plane distance distanceOffset = Mathf.Clamp(distanceOffset,0,distance); // prevent negative distance }else{ distanceOffset = 0; } }

This script is part of orbit camera but it can be applied to the follow script by setting the follow distance closer by the distanceOffset variable.

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 JeffAU · Mar 16, 2011 at 10:46 PM

Heya,

I'm having some troubles getting this script to work with SmoothFollow to stop the camera going through the terrain.

Any chance someone could post the completed Smooth Follow Script with this script merged into it?

I think my issue is here - trying to make use of the distanceOffset variable :

newdistance=distance-distanceOffset;
transform.position = target.transform.position;
transform.position -= currentRotation * Vector3.forward * newdistance;

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 whydoidoit · Mar 22, 2012 at 10:03 PM

I've written a version of a smooth follow camera that is terrain and structure aware. It might be some use... you can find it here

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

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

OnMouseDown on what? Camera Movement Help. 1 Answer

Camera to follow the mouse to move to create the point 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