Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UNITY3D: Change player control to a target model (FPS)

Tags:

c#

unity3d

I am developing a small game prototype in Unity 3.5.5f - in which the player controls a small mind controlling alien. The player needs to be able to take control of a target human NPC, switching all camera and controls to the human in question.

N.B. All of my code, thus far, is in C#.

I have two ideas on how to progress, which one is more feasible? (I'm happy to listen to alternative ideas)

  1. Each human in the level has a deactivated FPS controller script (and accompanying scripts). These scripts are activated when controlled (disabling the alien's scripts for the duration).
  2. Detach the current scripts from the alien and attach them to the target human.

The pros and cons as far as I seem them:

  1. Can have separate alien/human controls scripts (i.e. don't need to use States for input - e.g. can fire a gun whilst human, instead of melee as an alien on LMB). This method is very clusterfucky.
  2. This method is clean, but the code file for the player will be much larger as I cannot separate the input code as easily.

EDIT: A friend has pointed out, yes the NPCs have scripts of their own which would need to be disabled.

like image 648
Vikram Saran Avatar asked Oct 08 '12 01:10

Vikram Saran


1 Answers

This is pretty simple, in concept. Just have the NPC similar to the player class, in that it accepts control whenever something is true.
For example:

class NPC {
static bool isBeingControlled = false;
public void OnUpdate() {
    if (isBeingControlled)
    {
        //set camera position to NPC position (make sure you're using NPC as an instantiated class)
        //accept key input WASD or whatever you are using and move NPC according to input.
    }
}

}

You'll have to instantiate NPC for each NPC you have in your game.

like image 74
phxvyper Avatar answered Oct 29 '22 00:10

phxvyper