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 HeresChris · Nov 19, 2015 at 09:14 PM · movementgameobjectsfollowing

Make object go back to original spawn distance (Similar to Rust building system)

I currently have it so if they're looking down at the ground, the object won't go underneath the terrain, but, when the player looks back up, the object stays where it is instead of going back to the set position. I'm wondering how to make it so as the camera looks up, it also moves back to its original spot. Here's somewhat of an example of what I mean (best I could find (watch the foundation move)):

https://www.youtube.com/watch?v=QL0N7c5wYmo

The foundation has a set distance it stays away, but can also move closer if the player is looking towards the ground. I'm wonder how to put the foundation back to the set distance away if the player looks back up.

Comment
Add comment · Show 2
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 Thomas-Mountainborn · Nov 19, 2015 at 09:29 PM 0
Share

How is your system set up? It seems odd that the distance is decreased permanently once you look down. You could just use a raycast from the camera center with the max distance being the distance you want the object to be at normally, and if you hit something you position the object at that location, ins$$anonymous$$d of at the full distance.

avatar image HeresChris Thomas-Mountainborn · Nov 19, 2015 at 09:56 PM 0
Share

It's a child of the camera, but it also checks to see if it's this:

position < Terrain.activeTerrain.SampleHeight(transform.position) + 0.25f

and if it is, adjust the position. How would I set up the raycast to make it bounce back? I thought about using it, but couldn't really put it together.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Thomas-Mountainborn · Nov 20, 2015 at 07:45 PM

Alright, so instead of having the object be a child of the camera, it needs to be free of any parent. The object will then be positioned by raycasting from the camera center:

 public float DistanceFromCamera = 5, VerticalOffset = 0.25f;
 
     // Update is called once per frame
     void Update()
     {
         //Create a ray going from the center of the camera.
         Ray ray = Camera.main.ViewportPointToRay(new Vector2(0.5f, 0.5f));
 
         RaycastHit hit;
         //Cast the ray with the max distance.
         if(Physics.Raycast(ray, out hit, DistanceFromCamera))
         {
           /*If we hit something, we position the object above the point of the hit - you will have to check if the object we hit is actually the floor. Also, you will have to exclude the object itself from the raycast, using layers for instance, or by disabling its collider - you probably don't want it colliding with anything while it is being placed anyway. You will probably need to use Physics.RaycastAll instead, so the ray could pass through another player for instance, and still find its way to the terrain. I will leave this for you to figure out - it's real easy.*/
 
             transform.position = hit.point + new Vector3(0, VerticalOffset, 0);
         }
         else
         {
             //Else, if the ray does not intersect with anything, we simply position the object at the end of the ray - or you could choose to hide the object, for instance, if you only want people to place the object on the terrain.
             transform.position = ray.GetPoint(DistanceFromCamera);
         }
     }
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

How do I make a realistic jump? 1 Answer

objects in the same array wont move 0 Answers

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

Object "crawling" over floor, how? 2 Answers

How to make an object follow on 1 axis?? 1 Answer


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