private void UpdateVelocity() { CharacterController controller = GetComponent(typeof(CharacterController)) as CharacterController; Vector3 velocity = controller.velocity; if (firstframe) { velocity = Vector3.zero; firstframe = false; } if (grounded) { velocity = Util.ProjectOntoPlane(velocity, transform.up); } // Calculate how fast we should be moving Vector3 movement = velocity; //bool hasJumped = false; jumping = false; if (grounded) { // Apply a force that attempts to reach our target velocity Vector3 velocityChange = (desiredVelocity - velocity); if (velocityChange.magnitude > maxVelocityChange) { velocityChange = velocityChange.normalized * maxVelocityChange; } movement += velocityChange; // Jump if (canJump && Input.GetButton("Jump")) { movement += transform.up * Mathf.Sqrt(2 * jumpHeight * gravity); //hasJumped = true; jumping = true; } } float maxVerticalVelocity = 1.0f; AlignmentTracker at = GetComponent <AlignmentTracker>(); if (Mathf.Abs(at.velocitySmoothed.y) > maxVerticalVelocity) { movement *= Mathf.Max(0.0f, Mathf.Abs(maxVerticalVelocity / at.velocitySmoothed.y)); } // Apply downwards gravity movement += transform.up * -gravity * Time.deltaTime; if (jumping) { movement -= transform.up * -gravity * Time.deltaTime / 2; } // Apply movement CollisionFlags flags = controller.Move(movement * Time.deltaTime); grounded = (flags & CollisionFlags.CollidedBelow) != 0; }
void Start() { tr = GetComponent(typeof(AlignmentTracker)) as AlignmentTracker; Quaternion parentRot = neck.parent.rotation; Quaternion parentRotInv = Quaternion.Inverse(parentRot); referenceLookDir = parentRotInv * transform.rotation * headLookVector.normalized; referenceUpDir = parentRotInv * transform.rotation * headUpVector.normalized; lookDir = referenceLookDir; upDir = referenceUpDir; }
// Use this for initialization void Start () { //legA = GetComponent(typeof(LegAnimator)) as LegAnimator; align = GetComponent(typeof(AlignmentTracker)) as AlignmentTracker; cm = GetComponent(typeof(CharacterMotor)) as CharacterMotor; grounded = false; // Only use jumping if the jumping animation has ben set if (jumpingAnimation!=null) { animation[jumpingAnimation.name].wrapMode = WrapMode.ClampForever; doJumping = true; } // Only use idle animation if it has been set if (waitingAnimation!=null) { animation[waitingAnimation.name].wrapMode = WrapMode.ClampForever; doWaiting = true; } // Start with locomotion animation.Play("locomotion"); }
// Use this for initialization void Start() { //legA = GetComponent(typeof(LegAnimator)) as LegAnimator; align = GetComponent(typeof(AlignmentTracker)) as AlignmentTracker; cm = GetComponent(typeof(CharacterMotor)) as CharacterMotor; grounded = false; // Only use jumping if the jumping animation has ben set if (jumpingAnimation != null) { animation[jumpingAnimation.name].wrapMode = WrapMode.ClampForever; doJumping = true; } // Only use idle animation if it has been set if (waitingAnimation != null) { animation[waitingAnimation.name].wrapMode = WrapMode.ClampForever; doWaiting = true; } // Start with locomotion //animation.Play("locomotion"); }
void Start () { tr = GetComponent(typeof(AlignmentTracker)) as AlignmentTracker; legC = GetComponent(typeof(LegController)) as LegController; legs = legC.legs; if (!legC.initialized) { UnityEngine.Debug.LogError(name+": Locomotion System has not been initialized.",this); enabled = false; } legStates = new LegState[legs.Length]; updateStates = true; ResetMotionStates(); isActive = false; for (int leg=0; leg<legs.Length; leg++) { trajectories.Add( "leg"+leg+"heel", new TrajectoryVisualizer(legs[leg].debugColor, 3) ); trajectories.Add( "leg"+leg+"toetip", new TrajectoryVisualizer(legs[leg].debugColor, 3) ); trajectories.Add( "leg"+leg+"footbase", new TrajectoryVisualizer(legs[leg].debugColor, 3) ); } }
/// <summary> /// Calls the base Awake() function. /// </summary> public override void ControlledAwake() { tr = GetComponent(typeof(AlignmentTracker)) as AlignmentTracker; //NATHAN: I thought resetting was necessary. Apparently, it isn't. //tr.Reset(); }
void CopySerialized(Transform src, Transform tar) { if (src == null || tar == null) { return; } Rigidbody rigid = src.GetComponent <Rigidbody>(); if (rigid != null) { Rigidbody rigid1 = tar.GetComponent <Rigidbody>(); if (rigid1 == null) { rigid1 = tar.gameObject.AddComponent <Rigidbody>(); } EditorUtility.CopySerialized(rigid, rigid1); } CapsuleCollider cap1 = src.GetComponent <CapsuleCollider>(); if (cap1 != null) { CapsuleCollider cap2 = tar.GetComponent <CapsuleCollider>(); if (cap2 == null) { cap2 = tar.gameObject.AddComponent <CapsuleCollider>(); } EditorUtility.CopySerialized(cap1, cap2); cap2.center *= scale; cap2.radius *= scale; cap2.height *= scale; } SphereCollider s1 = src.GetComponent <SphereCollider>(); if (s1 != null) { SphereCollider s2 = tar.GetComponent <SphereCollider>(); if (s2 == null) { s2 = tar.gameObject.AddComponent <SphereCollider>(); } EditorUtility.CopySerialized(s1, s2); s2.center *= scale; s2.radius *= scale; } BoxCollider box1 = src.GetComponent <BoxCollider>(); if (box1 != null) { BoxCollider box2 = tar.GetComponent <BoxCollider>(); if (box2 == null) { box2 = tar.gameObject.AddComponent <BoxCollider>(); } EditorUtility.CopySerialized(box1, box2); box2.center *= scale; box2.size *= scale; } CharacterJoint joint1 = src.GetComponent <CharacterJoint>(); if (joint1 != null) { CharacterJoint joint2 = tar.GetComponent <CharacterJoint>(); if (joint2 == null) { joint2 = tar.gameObject.AddComponent <CharacterJoint>(); } EditorUtility.CopySerialized(joint1, joint2); joint2.connectedBody = AiUtil.GetChild(target.transform, joint2.connectedBody.name).GetComponent <Rigidbody>(); } LegController leg1 = src.GetComponent <LegController>(); if (leg1 != null) { LegController leg2 = tar.GetComponent <LegController>(); if (leg2 == null) { leg2 = tar.gameObject.AddComponent <LegController>(); } EditorUtility.CopySerialized(leg1, leg2); leg2.groundedPose = target.GetComponent <Animation>()[leg2.groundedPose.name].clip; leg2.rootBone = AiUtil.GetChild(target.transform, leg2.rootBone.name); foreach (LegInfo leg in leg2.legs) { leg.ankle = AiUtil.GetChild(target.transform, leg.ankle.name); leg.hip = AiUtil.GetChild(target.transform, leg.hip.name); leg.toe = AiUtil.GetChild(target.transform, leg.toe.name); leg.footLength *= scale; leg.footWidth *= scale; leg.footOffset.x *= scale; leg.footOffset.y *= scale; } foreach (MotionAnalyzer motion in leg2.sourceAnimations) { motion.animation = target.GetComponent <Animation>()[motion.animation.name].clip; } } AlignmentTracker al1 = src.GetComponent <AlignmentTracker>(); if (leg1 != null) { AlignmentTracker al2 = tar.GetComponent <AlignmentTracker>(); if (al2 == null) { al2 = tar.gameObject.AddComponent <AlignmentTracker>(); } EditorUtility.CopySerialized(al1, al2); } LegAnimator legA1 = src.GetComponent <LegAnimator>(); if (legA1 != null) { LegAnimator legA2 = tar.GetComponent <LegAnimator>(); if (legA2 == null) { legA2 = tar.gameObject.AddComponent <LegAnimator>(); } EditorUtility.CopySerialized(legA1, legA2); } }
private void UpdateVelocity() { if (controller == null || !controller.enabled) { return; } Vector3 velocity = controller.velocity; if (firstframe) { velocity = Vector3.zero; firstframe = false; } if (grounded) { velocity = Util.ProjectOntoPlane(velocity, Vector3.up); } // Calculate how fast we should be moving Vector3 movement = velocity; //bool hasJumped = false; jumping = false; if (grounded || gravity < PETools.PEMath.Epsilon) { // Apply a force that attempts to reach our target velocity Vector3 velocityChange = (desiredVelocity - velocity); if (velocityChange.magnitude > maxVelocityChange) { velocityChange = velocityChange.normalized * maxVelocityChange; } movement += velocityChange; if (desiredVelocity == Vector3.zero) { movement = Vector3.zero; } // Jump //if (canJump && false) //{ // //movement += transform.up * Mathf.Sqrt(2 * jumpHeight * gravity); // movement += Vector3.up * Mathf.Sqrt(2 * jumpHeight * gravity); // //hasJumped = true; // jumping = true; //} } float maxVerticalVelocity = 1.0f; AlignmentTracker at = GetComponent <AlignmentTracker>(); if (at != null && Mathf.Abs(at.velocitySmoothed.y) > maxVerticalVelocity) { movement *= Mathf.Max(0.0f, Mathf.Abs(maxVerticalVelocity / at.velocitySmoothed.y)); } Vector3 moveDeta = movement * Time.deltaTime; if (!CheckMoveDeta(moveDeta)) { movement = Vector3.zero; } // Apply downwards gravity //movement += transform.up * -gravity * Time.deltaTime; movement += Vector3.up * -gravity * Time.deltaTime; if (jumping) { //movement -= transform.up * -gravity * Time.deltaTime / 2; movement -= Vector3.up * -gravity * Time.deltaTime / 2; } // Apply movement CollisionFlags flags = controller.Move(movement * Time.deltaTime); grounded = (flags & CollisionFlags.CollidedBelow) != 0; }