Hi all, very new to unity and sorry if this has been asked previously but I’ve tried googling and searching the forums and haven’t found anything useful…
I’m trying to implement an object that when a player runs into it, causes the player to increase in speed temporarily, what is the best way to implement scripts like this in general? Should I check for collisions with gameObjects with a ‘player’ tag, or should I just write the script to react the same for any collision and only allow it to collide with the player using that matrix thing?
If I did this the second way how do I then get the collision game object as a player object where I can call a method to give the speed boost? Should I be using an interface for this?
Not sure on the best approach to take long term for components like this that I could reuse in future games
Thanks,
Chris
Okay, you’ll want to check the unity reference to see what objects need which components in regards to physics set up. It really just sounds more difficult than it actually is.
I would tell you but I don’t really work with the physics system often and I forget which object needs what but if I’m not mistaken, assuming you’re working on a 3D game, if you want the game object to be affected by gravity/physics you’ll add a rigidbody to it and if you want it to respond to collisions from OTHER objects, add a collider to it also.
So, if you need to move it via physics, add a rigidbody. If it’s a scenery object that doesn’t move, like a wall, floor, parked car, etc… then all you need to do is add a collider component to the object.
I’m not sure what the actual best practice is, but if you’re only interested in detecting when two objects come into contact with each other, I would use a collider and the OnTrigger methods. I generally use both the layer based physics detection and scripting to determine which objects react to each other.
For your specific situation I would add a collider to the object that increases the player’s speed when hit and a collider to the player. I would set the “IsTrigger” option of the speed object to true and add a script to the speed object. In the script use the OnTriggerEnter() method to increase the player’s speed.
Check out this part of the reference manual, I remember it being really confusing the last time I read through it but it seems like they’ve changed it since then and it’s pretty clear now. It does mention certain performance related issues with using non-primitive collider types but I don’t think that will be an issue.