How do make an object do damage to what it collides with, i was making an electric needle but it has to do damage CONSTANTLY on collision and is there a script to constantly create Game Objecs… on collision enter…?
Use Instantiate inside an… OnCollisionStay() function call.
ummmmmmmmm?
Notice the last part of your question “…is there a script to constantly create Game Objecs (sic) on collision enter”. From the doc entry for OnCollisionStay:
“OnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider.”
Thus Seon led you to a potential answer: use an OnCollisionStay function to see if your “needle” is hitting a target/damage receiver and if so, use Instantiate to continuously create game objects. Or similarly, you could use that function to continuously calculate damage.
Claelan, it would be helpful if you describe your problem more clearly. The more detail the better. Are you actually trying anything now? If so, a code snippet can sometimes provide more insight into what you are doing. Also, “Ummmmmm” gives no information as why Seon’s advice did not work for you, or even if you made an effort to use the advice. What did your effort using Seon’s advice actually do? Again, a code snippet here helps. Remember, we cannot read your mind or see what you are seeing.
Regarding CONSTANTLY, do you mean a level amount of damage over a period of time? If so, you might need to use time in your calculation if using OnCollisionStay() since OnCollisionStay() is called once per frame. That sounds like it is framerate dependent to me, which will vary over time. Another thing you could do is use a Coroutine to calculate the damage. Start the Coroutine using OnCollisionEnter() and use a yield to control the rate of damage. There is a tiny bit more, but not much if you use that route.
i am unable to make it intantiate an explosion, which would be easyst to just have it contantly instantiate an explosion.
Details please. What exactly is happening? What exactly is not happening that you think should? Can you post the code you’re using?
**************… :x
function OnCollisionStay(collisionInfo : Collision) {
// Debug-draw all contact points and normals
for (var contact : ContactPoint in collision.contacts) {
Debug.DrawRay(contact.point, contact.normal, Color.white);
}
}
I. Don’t. Know. How. To. Fix. This. Up!!
What’s the problem? Slow down and communicate clearly.
Describe the problem, describe what the code is NOT doing and describe what you expect it to be doing.
These one off outbursts aren’t going to get you closer to the answer you need.
+1
When you cite problems like this you should always provide the following information:
- clear information about what you want to have happen
- clear information about what you have attempted to try yourself
- clear information about exactly what is, or isn’t happening when you attempt to try yourself
Saying “I. Don’t. Know. How. To. Fix. This. Up!!” and using mad emoticons only shows you’re frustrated and angry (why are you angry? we’re here trying to help you…), but that fails to give us any new information so we can try to help which in turn frustrates us.
So let’s get to it: no more angry red/bold text, no more mad faces, just share the info we’re asking for so we can help…
Have you attempted to instantiate GameObjects in an OnCollisionStay function? If you have then can you please post that code.
When you use these scripts (either your own script that attempts to instantiate an explosion or the script example cited above) what’s happening on your end? Do you get script errors or other odd results? Please note that saying “it doesn’t work” doesn’t tell us anything, what does it do that indicates to you that it’s not working?
Where precisely do you need help? Do you understand how to use an OnCollisionStay function? Do you understand how to see which objects were involved in the collision? Do you understand how to use Instantiate? Etc.
Very roughly…
function OnCollisionStay(collisionInfo : Collision)
{
// This all assumes the object you're colliding with has it's own script attached to it that contains its health
var collisionObject = collisionInfo.gameObject;
var collisionScript = collisionObject.GetComponent(ScriptContainingTheHealth);
collisionScript.myHealth -= healthDecement;
// not instantiate the gameobject you want instantiated
Instantiate( someGameObject, somePosition, someRotation );
}
what does this script say??
Ah, bronxbomber beat me to it. One thing to keep in mind if instantiating an object in OnCollisionStay(), you will get a new object every frame, so you will get many really quick. Good thing is that your frame rate will probably drop quickly in this situation so the amount of objects being created per seconde will drop quickly Also, if applying damage there you damage will vary depending on frame rate since frame rate varies over time.
If you want a more predictable rate of damage application you could use a Coroutine, something like.
var delay = 0.1; // Apply damage every 10th of a second
var damageAmount = 3;
var continueDamage = false;
function OnCollisionStay(collisionInfo : Collision) {
if (collisionInfo.gameObject.tag = "tagToReceiveDamage")
{
Instantiate(explosionPrefab, transform.position, transform.rotation);
StartCoroutine("InflictDamage",collisionInfo.gameObject);
}
}
function InflictDamage(target : GameObject)
{
targetScript = target.GetComponent(DamageRecievingScript);
continueDamage = true;
while (continueDamage) {
targetScript.health -= damageAmount;
if targetScript.health <= 0
{
continueDamge = false;
}
yield WaitForSeconds(delay); // doh! left out the first time
}
// If you want to destroy the needle
Destroy(gameObject);
}
Added yield WaitForSecond() which was previously left out. Thanks bronxbomer
Looks like a nice script, but you never use the delay variable
I think you want to yield WaitForSecond(delay); at the end of the while loop, right?
unity is… :x
ive been working on the script because of the script the script, hey i see a pony! :?:
Very powerful and easy to learn especially if you utilize the helpful sorts that post here on these forums.
This thread has proven personally frustrating as you asked for help, many people (myself included) have offered replies with questions for more information, all of which (at least in my case) you’ve ignored. Yet after all of that you offer the above final thoughts so I’m not sure where to go from here. If you would like my/our help then why don’t you answer the many direct questions posted to you in this thread? For reference, here are the most recent questions I asked, all of which have gone unanswered:
Q: Have you attempted to instantiate GameObjects in an OnCollisionStay function? If you have then can you please post that code.
Q: When you use these scripts (either your own script that attempts to instantiate an explosion or the script example cited above) what’s happening on your end? Do you get script errors or other odd results? Please note that saying “it doesn’t work” doesn’t tell us anything, what does it do that indicates to you that it’s not working?
Q: Where precisely do you need help? Do you understand how to use an OnCollisionStay function? Do you understand how to see which objects were involved in the collision? Do you understand how to use Instantiate? Etc.
I can stand here all day offering my hand in assisting you, it’s up to you to reach out and grab that help by answering our questions about your goals, your level of understanding, what’s happening on your side, etc.
i think I understand what the original request was. so in my lunch break I put together this quick copy and paste hack fest. No doubt there is a cleaner way of doing this! But in the spirit of this forum I thought it best to supply something that is more or less double-click and run.
Obviously feel free to improve on this fugly hack.
Cheers.
Claelan: You also have to remember that doing this kind of stuff is not as simple as writing an email or similar stuff. This is game development.
No one here got it the first time. It takes practice, open listening and being open to actually learn how to do it from people who know how. (like all the helpful people who posted in this thread)
Unity makes things really easy compared to other game dev methods (C++ with a source-code engine. or rolling your own), but you have to remember it wont do everything for you.
It’s also unfair to expect us to write the code you want for you if you are not going to at least try to learn. It seems a lot of your posts are “gimmie” posts, which is fine, as long as you show us you are trying and are committed to learning. A lot of us on this forum are of the “…teach a man to fish, you feed him for a lifetime” mentality, so if you are not trying, we will get frustrated, and be less likely to help you.
-Jeremy
Ooop
how does needle jab work?