- Home /
How can I eliminate searching for a struct corresponding to the collider returned by OnCollisionEnter()
I have a player struct with a gameobject, transform, necessary values, and a weapon. I want to call OnCollisionEnter for my weapon from my player.
I am trying to think out how to detect a collision. I want to get the values from the player, the weapon, and the effected enemy without running a function that searches through names or something to find a match.
I'm not sure how abstract this is, feel free to ask questions
$$anonymous$$aybe I could have a pointer on my weapon object that points back to the struct of the player. Call OnCollisionEnter() but then I would still be searching through names to find the struct related to the effected enemy.
Are you trying to call a function on your gun when the player hits something or what is it exactly that you're trying to do gameplay wise? $$anonymous$$ight be easier to explain what outcome your looking for rather than how you're trying to get that outcome.
Is it possible to store a pointer in something that gets returned by OnCollisionEnter?
I'm not entirely sure what you mean by pointer but you can store pretty much anything and send it pretty much anywhere. I'm still unsure of what you're actually trying to do so I can't really help much. Do you want to send the collision information from when the player is hit by something to the weapon? Are you trying to call a function in the other when one is hit? Do you want to edit the variables in one with the other? Be specific and I can help :P
for this being asked too much already, you not showing that you've learned from that knowledge, and for weird tags.
Answer by Jessy · Oct 17, 2013 at 02:18 PM
You should learn how to use EventArgs. Fire an event in OnCollisionEnter, which contains the data you need. Subscribe to the event where necessary, and use the data as you see fit.
Sup! so I thought about it.
So I call OnCollisionEnter() in my weapon. I instantiate my weapon in my player and set a pointer"Owner" in my weapon to the player struct.
Right..
enemy's each have a script with another pointer that is set to their corresponding struct.
so on collision with enemy the weapon uses its "Owner" pointer to get info like damage and applies that to the enemy's health that we retrieve through collision.collider.GetComponent"script"().pointer.health;
its kinda like a web I guess
Although I answered this all too oftenly asked question myself, I will learn EventArgs.
Leroy Jenkins!!!
Yup, you can use GetComponent to call any function including OnCollisionEnter from another script.
Answer by rutter · Oct 18, 2013 at 12:26 AM
It's certainly possible to pass that collision event to another script -- use GetComponent
to find the other script, write a function which takes the data in, then call that function from the original object's OnCollisionEnter
.
For example, I once worked on a game where the player had multiple colliders: incoming projectiles could hit their shield, or their body. I had a simple component attached to each child collider, to detect the collisions, which could then pass them up to the main player script for processing.
Here's another UA thread which discusses messages and calls between scripts.
I would strongly recommend against generating your own collision data. Much easier to delegate.
Answer by Benjames · Oct 19, 2013 at 04:31 PM
So I couldn't change some variables in my structs from like a {get;set;} pointer like thing.. ya know that thing. Anyways I went looking around and found someone with that same problem. The solution was to change the structs into classes.
I changed over and painfully struggled to get it working until I found out that I had to set each class variable in an array to a new class object. Like I have two legs right.
pl.leg=new limb[2]; //Right?
so I had to go through each one and declare it again
pl.leg[0]=new limb();
pl.leg[1]=new limb();
I guess it was simple enough, with larger arrays its easy to just use a FOR statement. I had to do extra checks to make sure thing were initialized to like if(Owner!=null) aswell.
Anyway I got it all going, thanks meat, rutter, jessy, turkeybag!
Your answer
Follow this Question
Related Questions
array is pointer and float variable is not - why not? C# 1 Answer
Storage of types in Structs and Classes 1 Answer
Can I call OnCollisionEnter() for an object from another object? 0 Answers
Enemy Seeking Missiles 0 Answers
NativeList.GetUnsafePtr() throwing The type 'T' must be a non-nullable value type 1 Answer