Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Joe 3 · May 22, 2010 at 05:26 PM · charactercontrollerswitchflagged-for-close

Switching character controllers on key command?

I need to have a character walk up to an object and be able to press a button to switch character controllers. Anyone know how because I'm stumped.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Loius · Sep 29, 2010 at 05:09 AM

You want to detect that the player is near enough and that they're pressing a button -

in Vehicle.js:

private var directControl : boolean = false; // are we controlling this? var dismountPosition : Vector3; // where to respawn the player when dismounting var key_dismountVehicle : KeyCode; // key to press to dismount private var mainPlayer : Player; // the Player

function Start() { mainPlayer = GameObject.FindObjectOfType( Player ); // find Player }

function Update() { if ( !directControl ) return; // Do nothing if player not controlling this // handle player input if ( Input.GetKey( key_dismountVehicle ) { // when dismounting directControl = false; // no longer control this mainPlayer.AssumeDirectControl( this ); // tell Player to regain control from this vehicle } }

function AssumeDirectControl() { // player wants to drive directControl = true; }

then in your Player.js:

var key_mountVehicle : KeyCode; // key to press to enter vehicle

function Update() { if ( Input.GetKey( key_mountVehicle ) { // find a nearby vehicle // needs more code if vehicles can be near each other, as it may otherwise activate two at once var allVehicles : Array = GameObject.FindObjectsOfType(Vehicle); for ( var vehicle : Vehicle in allVehicles ) { if ( (vehicle.transform.position - transform.position).magnitude < vehicle.mountRange ) { vehicle.AssumeDirectControl(); // assume direct control of the nearby vehicle gameObject.active = false; // disable Player } } } }

function AssumeDirectControl( fromVehicle : Vehicle ) { // dismount vehicle // set player position to the vehicle's dismount position transform.position = fromVehicle.transform.position + vehicle.dismountPosition * vehicle.transform.rotation; gameObject.active = true; // reactivate player }

There's lots of room for improvement in there (lots), but that should be a good start.

Comment
Add comment · Show 1 · 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 Joe 3 · Nov 25, 2010 at 05:20 AM 0
Share

Thank you soooo much!

avatar image
1

Answer by HolBol · Jul 17, 2010 at 09:23 PM

Use

Gameobject.enabled = true
    Gameobject2.enabled = false

that's just the bare bones of a script but try to fit that in.

Comment
Add comment · 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
0
Wiki

Answer by user-1846 (google) · May 22, 2010 at 05:29 PM

Switching character controllers meaning like the FPSWalker script? The actual Character Controller itself doesn't do anything unless a script tells it to.

Comment your answer and I'll tell you the appropriate way to do this...

Christian

Comment
Add comment · Show 1 · 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 Joe 3 · Sep 25, 2010 at 11:15 PM 0
Share

like if a player were to walk up to a car and drive it

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

No one has followed this question yet.

Related Questions

Lerp can someone explain please 2 Answers

OnCollisionEnter does not work 4 Answers

Character falls thru to his waist 11 Answers

How to get movementDirection using CharacterController but without input.GetAxis ? 0 Answers

Collision with character controller and cube not working 1 Answer


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