Alright thought i would ask cause I am majorly lost in the hierchy of this engine.
These shouldnt trouble anyone with some experience. I have a small 2d sidescrolling shooter project. I could use some help with these questions.
1.- To create a projectile I believe i have to create a prefab for it. In it I would drop code to detect collision. What function is called when an object collides?
2.- Once the projectile collides, how would I go about deleteting it?
3.- Is there a way to modify the mesh used by a prefab?
thank you in advance for taking the time to at least read through this. I have scripting background just not in java and its my first unity project.
k Sweet. But i Seam to have an issue. The projectiles dont seam to call the OnCollisionEnter(). I created a basic spawn timer holder, and the update checks to see if 3 seconds have past then it destroys them so I know the destroy() works but im afraid the Collisionenter doesnt.
The prefab has, transform, Sphere, Sphere collider, meshrenderer, and my script.
here is the script in it:
var Speed : int = 10;
var LifeTime : int = 3;
var SpawnTime : float;
function Start ()
{
SpawnTime = Time.time;
}
function Update ()
{
transform.position.x += Speed * Time.deltaTime;
if(Time.time > SpawnTime + LifeTime)
Destroy(gameObject);
}
function OnCollisionEnter(collision : Collision)
{
// Destroy the projectile
Destroy(gameObject);
}
i have a simple cube in their path but they just go right through it, what am i doing wrong here?
k i didnt think i would need them. basically im not looking for a physics collision but an overlap of 2 objects. If 2 objects overlap there should be a function call or at least something to check that right?
this is a very basic game, no real physics needed.
From the documentation:
OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.
Note that collision events are only sent if one of the colliders also has a non-kinematic rigid body attached.
You do need colliders for both objects and at least one rigidbody amongst them
k so if i have a ship that moves, lets called it ship A. and I have an enemy ship, lets call it Ship B. Is there a way to get Ship A and Ship B to collide without changing angular velocities or anything like that?
I got the projectile to collide, but when i tried to add the rigin body to my ship, when it hits something it gets angular velocity and such and I really just want it to move like a side scrolling ship. is there some setting i can use to ignore collision forces? or is the work around just reset the actors rotation on update? should i always set the angular velocity and velocity to 0? This just seams overcomplicated for something that should be simple…
You can freeze the position and rotation if you don’t want it to move when it hits something. Otherwise, you can set the rigidbody to IsKinematic (which means you code your own physics) to be true and instead of using OnCollisionEnter (which requires non-kinematic rigidbody), you can use OnTriggerEnter (requires the collider that is calling the function to set IsTrigger to be true).
The problem with using OnTriggerEnter is that you cannot do really fast collision and it doesn’t provide collision info like OnCollisionEnter.
So basically there are no simple physics for this. Great, really not sure how anyone gets anything done in this thing. Seams so bare bone and overly complicated…
Try setting the “angular drag” on your rigidbodies up to 1.0, and physics interactions will give them only the barest rotational nudging. Then in code you can set their rotation to Quaternion.identity on a regular basis and the end-user will never notice that there’s any tomfoolery going on. (if you don’t set the angular drag, but you do force-change the rotations, you can end up with objects inside each other since physics rotated the object and moved the other object into the now-vacant space)
You can lower the rotational nudging by playing with masses - give your ship a mass of 1000 and no 1-mass bullet will ever do anything to it, physics-wise.
If you just want overlap, and no physics, then OnTrigger (and set the triger checkbox).
If you want to know where it hit precisely (angle/force/etc), then you need physics.
If you only care about which side it was hit on, you can use multiple colliders as triggers placed around it and parented.
There is also the question of whether a projectile is wanted at all (I think we all go down this line to start as we think - gun = bullet) - but there is also ther RayCast option.
If you want to see bare bones, try coding a game from scratch, without a game engine.
Once you do that (or attempt to), you will realize how much Unity actually does for you.
I guess i just expected more from an engine thats available for purchase. Seams like features that other engines offer would have to be built from the ground up.
Im just tring to do a simple game like arkanoid. I sone it in torque2d and udk with ease.