Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lemdi · Sep 03, 2011 at 09:17 AM · cameracollidercharactercontrolswitch

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 !

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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".

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lemdi · Sep 03, 2011 at 06:59 PM 0
Share

thank you i may need more help but i will se how well it works

avatar image BinaryCaveman · Sep 03, 2011 at 07:04 PM 0
Share

$$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. :)

avatar image lemdi · Sep 04, 2011 at 09:52 AM 0
Share

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

avatar image lemdi · Sep 04, 2011 at 12:45 PM 0
Share

i am trying hard to do it on my own

avatar image BinaryCaveman · Sep 05, 2011 at 04:45 AM 0
Share

I have updated my answer above, I hope it helps a little.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges