- Home /
Adding a Character Controller using c# script
Hello,
I'm trying to get a FPS controller to snap onto and off of any model on I have on my little chunk of test terrain. The FPS controller works great if you add it to a model and then adjust it manually in the window, but when you use addcomponent it seems to have some issues.
1) Model has to be far above terrain or it just falls through. So the model is floating and moving way above the terrain. 2) camera is at the models feet. 3) can't move the components or it just moves the model so can't figure out how to move the camera to face level.
Any ideas?
using UnityEngine;
using System.Collections;
public class ToonControl : MonoBehaviour {
GameObject small;
GameObject medium;
GameObject large;
// Use this for initialization
void Start () {
small = GameObject.FindGameObjectWithTag ("small");
medium = GameObject.FindGameObjectWithTag ("medium");
large = GameObject.FindGameObjectWithTag ("large");
}
// Update is called once per frame
void Update () {
}
void OnGUI () {
GUI.Box (new Rect (10, 10, 120,110), "Possess Minion");
if(GUI.Button (new Rect(30,40,80,20), "Small")) {
camera.enabled = false;
small.AddComponent<Camera> ();
small.camera.tag = "MainCamera";
small.AddComponent<CharacterController>();
small.AddComponent<MouseLook>();
small.AddComponent<FPSInputController>();
}
if(GUI.Button (new Rect(30,65,80,20), "Medium")) {
camera.enabled = false;
medium.AddComponent<Camera> ();
medium.camera.tag = "MainCamera";
}
if(GUI.Button (new Rect(30,90,80,20), "Large")) {
camera.enabled = false;
large.AddComponent<Camera> ();
large.camera.tag = "MainCamera";
}
}
}
Answer by crunchyoverseas · Aug 02, 2014 at 06:41 PM
Stumped?
Okay, I'll give you the solution:
Create an empty gameobject with the same coordinates as your model. If you model is scale 6x6x6, make the scale of your empty gameobject something like 1x5x1. But you want the Y to be just a little smaller so you are at face level. Also make sure you rotated the correction direction. Add your model to the empty game object and it works. It will take some playing around to get it just right. But the collider will keep it from falling through the world and it won't be floating above the ground.
If I get some time I might post a small video on how to do it, I've several people ask the same question and not get an appropriate response back. I asked it like 3 times and finally got half an answer which allowed me to fill in the other half of the equation.
I really like the video tutorial section but I feel they should have some shorter, smaller videos where they just tackle some of these often asked questions. Unity should hire me to make those :)
Your answer