- Home /
Question by
MrGamingdwarf · Sep 23, 2013 at 01:14 AM ·
move an object
Replace A Component Then Crashes
Using Unity 4.2.1f4. Whenever I add my script which requires a Character Controller but when I add the script Unity ask to replace the current collider, I click add or replace and it crashes. Am I doing something wrong? I am new to Unity. Here my MovementController script:
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(CharacterController))]
[RequireComponent (typeof(Animation))]
[RequireComponent (typeof(PlayerInput))]
public class MovementController : MonoBehaviour {
public enum State {
Idle,
Init,
Setup,
Run
}
public enum Rotate {
left = -1,
none = 0,
right = 1
}
public enum Move {
backward = -1,
none = 0,
forward = 1
}
private Transform myTransform;
private CharacterController myController;
private Vector3 myDirection;
private CollisionFlags myCollisionFlags;
public float RotationSpeed = 100;
public float MovementSpeed = 5;
public float StrafeSpeed = 3;
public float Gravity = 20;
public float AirTime = 0;
public float FallTime = 0.5f;
public float JumpHeight = 8;
public float JumpTime = 1.5f;
public AnimationClip RotateAnimation;
public AnimationClip MoveAnimation;
public AnimationClip StrafeAnimation;
public AnimationClip JumpAnimation;
public AnimationClip FallAnimation;
public bool Jump;
private Rotate rotate;
private Move move;
private Rotate strafe;
private State state;
void Awake() {
myTransform = transform;
myController = GetComponent<CharacterController>();
state = State.Init;
}
IEnumerator Start () {
while(true) {
switch(state) {
case State.Init:
Init();
break;
case State.Setup:
Setup();
break;
case State.Run:
ActionPicker();
break;
}
yield return null;
}
}
void Init() {
if(!GetComponent<CharacterController>()) return;
if(!GetComponent<Animation>()) return;
state = State.Setup;
}
void Setup() {
myDirection = Vector3.zero;
rotate = Rotate.none;
move = Move.none;
strafe = Rotate.none;
Jump = false;
state = State.Run;
}
void ActionPicker() {
if(rotate != Rotate.none) {
myTransform.Rotate(0, (int)rotate * Time.deltaTime * RotationSpeed, 0);
Rotating();
}
if(myController.isGrounded) {
AirTime = 0;
myDirection = new Vector3((int)strafe, 0, (int)move);
myDirection = myTransform.TransformDirection(myDirection).normalized;
myDirection *= MovementSpeed;
if(move != Move.none) {
Moving();
}else if(strafe != Rotate.none) {
Strafing();
}else{
Idle();
}
if(Jump) {
if(AirTime < JumpTime) {
myDirection.y += JumpHeight;
Jumping();
Jump = false;
}
}
}else{
if((myCollisionFlags & CollisionFlags.CollidedBelow) == 0) {
AirTime += Time.deltaTime;
if(AirTime > FallTime) {
Falling();
}
}
}
myDirection.y -= Gravity * Time.deltaTime;
myCollisionFlags = myController.Move(myDirection * Time.deltaTime);
}
void MoveAction(Move z) {
move = z;
}
void RotateAction(Rotate y) {
rotate = y;
}
void StrafeAction(Rotate x) {
strafe = x;
}
void JumpAction() {
Jump = true;
}
void Rotating() {
}
void Moving() {
}
void Strafing() {
}
void Jumping() {
}
void Falling() {
}
void Idle() {
}
}
Comment
Your answer
Follow this Question
Related Questions
Rotating an Object while Moving 0 Answers
how to work my game 0 Answers
Object rotating around object at two levels 1 Answer
Translate speed keeps changing 1 Answer