- Home /
Instantiating exactly above the ground
hello everyone!
Im trying to simulate digging on the ground, the player has a 'digging' animation, then a soil prefab with a texture of dirt will instantiate in front of him, this soil will grow every second to make it look that the dirt is piling up.
so far this it what i have, the soil has a rigid body so it will drop on the ground, im having some problem because sometimes, that soil will go through under the terrain
old script
//player.js function dig () { Instantiate (Soil, (transform.position + (transform.forward * 1.1)) +(transform.up * -1), Quaternion.identity); }
//soil.js function grow() { for ( i = 0 ; i <= 10 ; i++ ) { transform.localScale.x += 0.1; transform.localScale.z += 0.1; yield WaitForSeconds(0.5); } Destroy (rigidbody); Destroy (this); }
function Awake () { grow(); }
new script
//player.js
static var hitpass : Vector3; //this will be use on soil.js for the surface normal function dig () { var hit : RaycastHit; var ground = transform.TransformDirection(-Vector3.up);
if(Physics.Raycast(transform.position, ground , hit, 10))
{
hitpass = hit.normal;
Instantiate (Soil, hit.point, Quaternion.identity);
}
}
//soil.js function grow() { for ( i = 0 ; i <= 10 ; i++ ) { transform.localScale.x += 0.1; transform.localScale.z += 0.1; yield WaitForSeconds(0.5); } Destroy (this); }
function Awake () { transform.up = player.hitpass; //passes the normal during the raycast from player.js grow(); }
Answer by spinaljack · May 02, 2010 at 10:11 AM
Instead of giving the soil a rigid body you can use raycast to find exactly the point on the surface you want the soil to appear at.
When your character is digging, do a raycast a small distance in front of him pointing down, use the ray cast hit point data to instantiate the soil. You can even find the incline of the terrain and spawn the soil at the proper angle.
Use the normal of the surface as the up direction of your soil
EDIT
A method of changing a vector to a Quaternion is with the set look rotation function. The you stick the quaternion in the last argument of the instantiate function. You may need to rotate your soil model to face upwards.
hello, i successfully simulated the digging using a raycast, i updated the script above. i'm really interested at this "You can even find the incline of the terrain and spawn the soil at the proper angle." can you please tell me more about it? how can i get the rotation of the hit.point? i've tried different angle variables, nothing works so far.
it is now working perfectly! thank you very much. . but! :) i had to pass the raycast.normal data to the soil's script, do you think its necessary? is there a way to apply the raycast.normal during instantiating? i updated the code above.
You need to convert the vector into a Quaternion and then plug it into the instantiate function if you didn't want to pass any data
Your answer
![](https://koobas.hobune.stream/wayback/20220612203435im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Script that allows me to spawn enemies in game. 1 Answer
Position in front of an object? 2 Answers
Checking if object intersects? 1 Answer
instantiate objects in menu 1 Answer
How do I revert prefab instance properties to their defaults? 0 Answers