- Home /
character switching on collision
Hi i am only new to unity and i still am getting the hang of it. I am making a game where if the ball collides with character1,character2,character3,character4 or character5 then you change controlls and camera to that character. Please help i will be most gratefull if you can !
Answer by BinaryCaveman · Sep 03, 2011 at 06:33 PM
First, you should add a collider to the ball and each character, and change the `Is Trigger` option for each character collider to on, using the inspector.
Then, create a script, attached to each GameObject, which uses `OnTriggerEnter()` to detect collisions.
Inside each character's OnTriggerEnter()
function, you should insert code which would turn off the main camera and on that character's individual camera.
A global variable could be used to determine which GameObject (ball or character) should respond to user interaction (controls). It could be something as simple as a variable which stores a GameObject.
Controls.js
static var currentObject : GameObject = GameObject.Find("Ball");
// stores the current GameObject, which is set to the ball GameObject in the beginning.
Character.js
static var ball : GameObject = GameObject.Find("Ball"); // store the ball GameObject for use later...
function OnUpdate() {
if (Controls.currentObject == gameObject) { // if this is the current object
// respond to controls
}
}
function OnTriggerEnter(other : Collider) {
if (other.gameObject == ball) { // check for collision with ball
Controls.currentObject = gameObject; // transfer focus to me!
// set camera to this GameObject's camera, etc.
}
}
I will leave the implementation details up to you, unless you need help, of course, in which case I will gladly help you.
Edit
First, create a new JavaScript script and name it something like "Character.js". Inside of it, after the automagically created OnUpdate()
function, type the following:
function OnTriggerEnter(other : Collider) {
if (other.gameObject == ball) { // check for collision with ball
Controls.currentObject = gameObject; // transfer focus to me!
// set camera to this GameObject's camera, etc.
}
}
In place of // set camera to this GameObject's camera, etc.
, turn the main camera off (find the camera component using GetComponent()
, set its .enabled = off
) and a camera attached to the current character on, using similar methods.
For the creation of a global variable, create a new script (JS again) and name it something like "Controls.js", and copy the code from above into it. Using this, you can reference its variable, which by placing a static
keyword in front of it, is global, by using something like Controls.currentObject
. As in the OnUpdate()
function I included above, you can check whether this object will respond to movement, and respond accordingly, by placing an `Input.GetButton()` inside. Put that function inside "Character.js".
thank you i may need more help but i will se how well it works
$$anonymous$$eep in $$anonymous$$d this is just an outline of how the code could be. There is not much "real" code here. I thought that, since you said you are new to Unity, working on the implementation details on your own would be a good exercise. :)
as i said i dam very very new to unity and i am struggling with this bit of helpfull info you gave me : Then, create a script, attached to each GameObject, which uses OnTriggerEnter() to detect collisions. Inside each character's OnTriggerEnter() function, you should insert code which would turn off the main camera and on that character's individual camera.
A global variable could be used to deter$$anonymous$$e which GameObject (ball or character) should respond to user interaction (controls). It could be something as simple as a variable which stores a GameObject
be grateful if you could help thank you
I have updated my answer above, I hope it helps a little.
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
How to control 2 characters 1 Answer
View and control script broken? 1 Answer
switch character 1 Answer
Triggers not changing cameras 2 Answers