- Home /
How to change what the user is controlling?
I am working on a game and trying to straighten out something basic things. Right now I am up against the task of vehicles. So I have written several controller scripts. Lets say the player is walking and finds a car. Now we want to get in it. So the player walks up to it and a message is displayed saying "Press E to enter". Then what happens?
How can I disable the player completely, enable the vehicle(Controller, camera, and other scripts attached) to be what the user is controlling now. Then when able, press E again to exit. How would I go about doing this? Because I have no idea where to start.
Answer by Fornoreason1000 · May 25, 2014 at 03:35 AM
Ok there are lots of ways around this one. personally I do not know which is best for your case but I'll how you one way to get you started;
basically you want to check that if the player is about to enter the car the disable him/her motion code. then tell you camera scripts to either go into "car mode" or replace them with your CarCamera Scripts. both of these can be done with boolean variable checking and Add/Remove Components functions.
After that , you want the car to be controller, this is where another Addcomponent function will come in. you basically add your car control script to the car your player just got into.
Alternatively... you can pass both Players And cars to be "conmtrollable" objects, that recive inputs external to a certain instance, whenever you change between the two will cause the other to be disabled.
Heres a example of a way of coding that, NOTE copying and pasting this code Will not work as it is for demonstration purposes.
abstract class ControllableObject : Monobeahviour {
public void GamePadInputs(Vector2 Leftstic, Vector2 rightStick);
}
//Manages the player globally
public class PlayerManager : Monobehaviour {
public static ControllableObject player;
public void Update() {
//place your left and right stick variables in this code
player.GamePadInputs(myLeftstick , myRightStick }
}
}
//You player, the one with two legs
public class Player : ControllableObject {
//Gets called by PlayerManager
public override void GamePadInputs(Vector2 Leftstic, Vector2 rightStic) {
//yor movement
moveDir = leftstic;
}
}
//your car class, the one with four wheels
public class Car : ControllableObject {
//gets called inside PlyuerManager if this class is currently marked as player
public override void GamePadInputs(Vector2 Leftstic, Vector2 rightStic) {
Forwardaccel = Leftstic.y;
}
}
when stuck on logic probelms try drawing flowcharts or block diagrams to guide you. break them down as much as you can and convert them into code.
Let me know of you need more info
please post comments as comments ans Answers as Answers( I didn't down vote you btw but its tempting ).
you're welcome nonetheless