- Home /
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.
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.
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.
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);
}
}
Your answer
Follow this Question
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