- Home /
How do I script a script to put on a static game object, to detect when the character controller is colliding with it?
I have my Character Controller, it has a script on it which does a few things and all works fine, however I need to code it so that it works when the Script is on the static game object and not the character controller.
The problem I have ran in to is with the Collision, I have made it work, to an extent, with OnTriggerEnter after making the static game object an isTrigger, though of course the character controller falls through it.
For now I am going to try and create an alternative method but any help would be greatly appreciated :)
Answer by Mmmpies · Jan 03, 2015 at 06:38 PM
OnTriggerEnter is used if the collider on your object is a trigger.
Try unticking the is trigger and use OnCollisionEnter instead if the issue is that you actually want it to collide.
I actually have found a different solution to my problem, however the answer to this specific problem would still be useful to know.
$$anonymous$$mmpies, thanks for the answer, though from what I understand OnCollisionEnter only works if the object that the script is attached to is moving, for a static object it is not colliding with anything, just being collided with.
OnCollisionEnter = OnTriggerEnter
OnCollisionStay = OnTriggerStay
OnCollisionExit = OnTriggerExit
If you were using OnTriggerEnter but not colliding was a problem then OnCollisionEnter is the solution.
If that isn't the solution then can you clarify the issue and how you fixed it, it might help someone else which is what Unity Answers is all about.
Really if you think we've answered your question then click that it's answered and if you have a different answer then post your solution so other people can benefit.
I'm not complaining, I don't really care about karma points but just try and make this a question that has an answer so it helps others.
Hey, yeah that's a good idea.
I haven't found the solution to this question, however I found the solution to my problem.
Say I have platform A, platform B, door A and door B. I want door A to open when I am standing on Platform A, and door B to open when I am standing on platform B. Though it was a bit more complicated than that.
Reasonably easily I can write a script for each and attach them to my character controller, however I do not want to crowd my character controller with a lot of scripts.
So I decide to put the scripts of the platforms ins$$anonymous$$d, however I could not make the platform detect the Collision with the character controller, which is where this question was created, the solution above I am fairly certain I have tried before but I will test it again soon. It would be good for everyone to know.
What I ended up doing, was create one script on the character controller, this would then would then get component the script on the collided object upon collision, and set a boolean in the script to true, after storing the collided game object, I could then set the boolean to false using a similar method when colliding with another gameObject. Everything for the scripts already attached to the gameObjects was of course already in place to work.
The script ended up like this:
sing UnityEngine;
using System.Collections;
public class PlatSlide$$anonymous$$aster : $$anonymous$$onoBehaviour {
private PlatformSlideStatic PlatRef;
private PlatformSlide$$anonymous$$ove $$anonymous$$oveRef;
public bool elseWhere;
GameObject Platform;
// Use this for initialization
void Start () {
elseWhere = true;
}
void OnControllerColliderHit (ControllerColliderHit warp)
{
if (warp.gameObject.name == "WarpPlat")
{
Platform = warp.gameObject;
PlatRef = Platform.GetComponent<PlatformSlideStatic> ();
PlatRef.OnWarpPlatform = true;
elseWhere = false;
}
else if (warp.gameObject.name == "CollPlatform")
{
elseWhere = true;
PlatRef = Platform.GetComponent<PlatformSlideStatic> ();
PlatRef.OnWarpPlatform = false;
$$anonymous$$oveRef = warp.gameObject.GetComponent<PlatformSlide$$anonymous$$ove>();
$$anonymous$$oveRef.ToEnd();
}
else if (elseWhere == false)
{
elseWhere = true;
PlatRef = Platform.GetComponent<PlatformSlideStatic> ();
PlatRef.OnWarpPlatform = false;
}
}
}