- Home /
Assigning character controller in-script and efficiency of scripting
I'm currently working on my first movement script. I decided to use the character controller function component as the method of action. I know general basics, but terminology and efficiency can sometimes escape me. Here is what I have so far:
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour
{
public CharacterController charController;
public float speed;
private Vector3 moveDirection = Vector3.zero;
// Use this for initialization
void Start ()
{
charController = GetComponent<CharacterController>(StoneKing);
}
// Update is called once per frame
void Update ()
{
moveDirection = new Vector3(Input.GetAxis ("Horizontal"),0,Input.GetAxis("Vertical"));
transform.TransformDirection(moveDirection);
charController.Move(moveDirection);
}
}
The Character Controller needs assigned to the parent object StoneKing (of which has a mesh child object named StoneKing if that matters?). Also, is the update function (correct term?) the right place to put those three lines? Is there a nifty place to squirrel them away like I did with the GetComponent function? Thanks :)
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Changing Mesh of an object depending on Health Value. C# 2 Answers
Pushing More GUI Clips Than Popping 0 Answers
Trouble using C# tooltips 1 Answer