- Home /
How do you make a type of excavation when a gameobject collides with another gameobject?
I am making a sort of space sim. In my game, you control a ship and I need to figure out how to make it so that you fire projectile rigidbody spheres at cube-like meshes (with rigidbodies?), that are "asteroids". How can I make it so that when you shoot at one of these meshes, it has an indent at where you shot, and can eventually fully disappear when you have shot it enough. This should be somewhat like in space engineers.
Answer by Kacheek · May 07, 2014 at 02:37 PM
well this is a bit more complex since youre actually asking several questions ...
fire a sphere projectile
first create the sphere you want to shoot than save it as a prefab you can than spawn it with Object.Instantiate ( see https://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html)
so when you click you should execute that instantiate command of course include the position where you want it to spawn (in front of your blaster i guess ?) and assign the sphere prefab to it
you will then at first only have a sphere in front of your blaster i would suggest to use Force to let the sphere move (https://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html)
also dont forget to turn off "Usergravity" since you are in space theres no gravity! it will fall down if you dont do that.
the sphere and your cube asteriod should both have rigidbodys with no gravity andn both should have colliders that match their appearance!
you should also assign a proper mass for example an asteroid may weight up to some tons! the mass field of the rigidbody component is the objects weight in kg also keep in mind if you let your blaster sphere only have a mass of 1 and the asteroid has 1000000 it wont affect the asteroid it should be something like 10 and 100 i think... (just a guess you will ahve to experiment with different values)
so now you have a blaster that gets shot and it may hit the asteroid and both will move properly afterwards the indent is kinda complex so i skip this for now
for it to dissapper you could assign hp to it and if it gets hit it will lose 1 hp each so if you hit it lets say 3 times it will be destroyed
the indent thing could be done with adding bones to your cube mesh and move each bones or something i dont know an easy way for that sorry
i hope i could help you a bit
best regards
That seems to have answer most of my question, but do you know how I can use bones to make indents? It seems very simple, yet I can't quite grasp it.
let the asteroids bone that is the closest to the colliding point with your projectile move inverted to the center of your asteroidd object for a certain amount or just do
if(boneishit)
{
bonetransform.localposition = new Vector3 (0,0,0);
}
this will let it move to the actual center of it i think it will get invisible if you hit all bones too just try it out ;)
but well it my look strange
but a better looking solution would be to do this here:
this will make a smoth movement of the bone if it gets hit towards the center and will stop if its halfway to the center
should look better
if you got issues understanding this code check
https://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html
Vector3 startposition;
Vector3 newposition;
bool boneIsHIT = false;
float journeyLength = 0f;
float distCovered = 0f;
float fracJourney = 1f;
float speed = 1f;
float startTime = 0f;
void start()
{
}
void Update ()
{
// if you set speed to 1 it will move 1unit per sec
if(boneIsHit)
{
boneIsHit = false;
//this should only be executed once!
startposition = bonetransform.position;
// we dont want it to move to the center so we half the difference
// and take it the half away so we only move half towards the
// center
newposition = cubetransform.position -
(Vector3.Distance(cubetransform.position,bonetransform.position) / 2);
// $$anonymous$$eep a note of the time the movement started.
startTime = Time.time;
// Calculate the journey length.
journeyLength = Vector3.Distance(startposition, newposition);
}
// Distance moved = time * speed.
distCovered = (Time.time - startTime) * speed;
// Fraction of journey completed = current distance divided by total distance.
fracJourney = distCovered / journeyLength;
if(fracJourney <= 1)
{
bonetransform.position = Vector3.Lerp(start$$anonymous$$arker.position, end$$anonymous$$arker.position, fracJourney);
}
}