Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This makes the character turn to face the current movement speed per default.
- var autoRotate : boolean = true;
- var maxRotationSpeed : float = 360;
- var character : SmoothMoves.BoneAnimation;
- //var wayPoints : Transform[];
- //var alertRange : int;
- //var attackRange : int;
- //var target : Transform;
- //var facingRight : boolean;
- //var idleDuration : float;
- //
- private var motor : NPCMotor;
- private var moveLeft : boolean;
- private var moveRight : boolean;
- //private var reachPoint : boolean;
- //
- //private var currentWayPoint : int;
- //private var lastArrive : float;
- //private var isFacingRight : boolean;
- //private var idleing : boolean;
- //private var idleStart : float;
- //
- //private var goAttack : boolean;
- // Use this for initialization
- function Awake () {
- // secondStage = false;
- // moveLeft = false;
- // moveRight = false;
- motor = GetComponent(NPCMotor);
- NoMove();
- // currentWayPoint = 0;
- // lastArrive = Time.time;
- // isFacingRight = facingRight;
- // idleing = false;
- // goAttack = false;
- }
- //public function Attack()
- //{
- // goAttack = true;
- //}
- //function AI()
- //{
- // //if player is in NPC's watching range
- // if(Vector3.Distance(target.position, transform.position) <= alertRange || goAttack)
- // {
- // idleing = false;
- // if(Mathf.Abs(target.position.x - transform.position.x) <= attackRange)
- // {
- // Debug.Log("tell: " + Mathf.Abs(target.position.x - transform.position.x));
- // character.Play("IDLE_side");
- // NoMove();
- // }
- // else if(target.position.x < transform.position.x)
- // {
- // character.Play("Walk_side");
- // if(isFacingRight)
- // transform.localScale.x *= -1;
- // isFacingRight = false;
- // LeftMove();
- // }
- // else if(target.position.x > transform.position.x)
- // {
- // character.Play("Walk_side");
- // if(!isFacingRight)
- // transform.localScale.x *= -1;
- // isFacingRight = true;
- // RightMove();
- // }
- // }
- // else
- // {
- // if(idleing)
- // {
- // if(Time.time - idleStart >= idleDuration)
- // {
- // idleing = false;
- // }
- // }
- // else if(Mathf.Abs(wayPoints[currentWayPoint].position.x - transform.position.x) <= 10.0f &&
- // Time.time - lastArrive > 1.0f)
- // {
- // character.Play("IDLE_side");
- // lastArrive = Time.time;
- // idleing = true;
- // idleStart = Time.time;
- // NoMove();
- // if(currentWayPoint == 0)
- // currentWayPoint = 1;
- // else
- // currentWayPoint = 0;
- // }
- // else if(wayPoints[currentWayPoint].position.x < transform.position.x)
- // {
- // character.Play("Walk_side");
- // if(isFacingRight)
- // transform.localScale.x *= -1;
- // isFacingRight = false;
- // LeftMove();
- // }
- // else if(wayPoints[currentWayPoint].position.x > transform.position.x)
- // {
- // character.Play("Walk_side");
- // if(!isFacingRight)
- // transform.localScale.x *= -1;
- // isFacingRight = true;
- // RightMove();
- // }
- // }
- //}
- //Call by playMaker
- function ManuallyAttack()
- {
- }
- function LeftMove (speed : float)
- {
- motor.LeftMove (speed);
- moveLeft = true;
- moveRight = false;
- }
- function RightMove (speed : float)
- {
- motor.RightMove (speed);
- moveLeft = false;
- moveRight = true;
- }
- function NoMove ()
- {
- motor.NoMove ();
- moveLeft = false;
- moveRight = false;
- reachPoint = false;
- }
- function StopMove()
- {
- motor.NoMove ();
- moveLeft = false;
- moveRight = false;
- reachPoint = true;
- }
- // Update is called once per frame
- function Update () {
- // Get the input vector from kayboard or analog stick
- var moveRate : float;
- // AI();
- // if(moveRight)
- // {
- // moveRate = 1.0f;
- // }
- // else if(moveLeft)
- // {
- // moveRate = -1.0f;
- // }
- // else
- // {
- // moveRate = 0.0f;
- // }
- var directionVector = new Vector3(moveRate, Input.GetAxis("Vertical"), 0);
- if (directionVector != Vector3.zero) {
- // Get the length of the directon vector and then normalize it
- // Dividing by the length is cheaper than normalizing when we already have the length anyway
- var directionLength = directionVector.magnitude;
- directionVector = directionVector / directionLength;
- // Make sure the length is no bigger than 1
- directionLength = Mathf.Min(1, directionLength);
- // Make the input vector more sensitive towards the extremes and less sensitive in the middle
- // This makes it easier to control slow speeds when using analog sticks
- directionLength = directionLength * directionLength;
- // Multiply the normalized direction vector by the modified length
- directionVector = directionVector * directionLength;
- }
- // Rotate the input vector into camera space so up is camera's up and right is camera's right
- directionVector = Camera.main.transform.rotation * directionVector;
- // Rotate input vector to be perpendicular to character's up vector
- var camToCharacterSpace = Quaternion.FromToRotation(-Camera.main.transform.forward, transform.up);
- directionVector = (camToCharacterSpace * directionVector);
- // Apply the direction to the CharacterMotor
- motor.inputMoveDirection = directionVector;
- motor.inputJump = Input.GetButton("Jump");
- // Set rotation to the move direction
- if (autoRotate && directionVector.sqrMagnitude > 0.01) {
- var newForward : Vector3 = ConstantSlerp(
- transform.forward,
- directionVector,
- maxRotationSpeed * Time.deltaTime
- );
- newForward = ProjectOntoPlane(newForward, transform.up);
- transform.rotation = Quaternion.LookRotation(newForward, transform.up);
- }
- }
- function ProjectOntoPlane (v : Vector3, normal : Vector3) {
- return v - Vector3.Project(v, normal);
- }
- function ConstantSlerp (from : Vector3, to : Vector3, angle : float) {
- var value : float = Mathf.Min(1, angle / Vector3.Angle(from, to));
- return Vector3.Slerp(from, to, value);
- }
- // Require a character controller to be attached to the same game object
- @script RequireComponent (NPCMotor)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement