x


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.

more ▼

asked May 22 '10 at 05:26 PM

Joe 3 gravatar image

Joe 3
33 3 3 8

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Sep 29 '10 at 05:09 AM

Loius gravatar image

Loius
10.7k 1 11 41

Thank you soooo much!

Nov 25 '10 at 05:20 AM Joe 3
(comments are locked)
10|3000 characters needed characters left

Use

Gameobject.enabled = true
    Gameobject2.enabled = false

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

more ▼

answered Jul 17 '10 at 09:23 PM

Fishman92 gravatar image

Fishman92
2.4k 101 113 128

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered May 22 '10 at 05:29 PM

user-1846 (google) gravatar image

user-1846 (google)
216 6 6 13

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

Sep 25 '10 at 11:15 PM Joe 3
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x675
x237
x122

asked: May 22 '10 at 05:26 PM

Seen: 1934 times

Last Updated: Jul 17 '10 at 09:12 PM