public virtual void Update() { ThirdPersonController playerController = (ThirdPersonController)this.GetComponent(typeof(ThirdPersonController)); float currentSpeed = playerController.GetSpeed(); if (currentSpeed > playerController.walkSpeed) { this.anim.CrossFade("run"); this.anim.Blend("jumpland", 0); } else { if (currentSpeed > 0.1f) { this.anim.CrossFade("walk"); this.anim.Blend("jumpland", 0); } else { this.anim.Blend("walk", 0f, 0.3f); this.anim.Blend("run", 0f, 0.3f); this.anim.Blend("run", 0f, 0.3f); } } this.anim["run"].normalizedSpeed = this.runSpeedScale; this.anim["walk"].normalizedSpeed = this.walkSpeedScale; if (playerController.IsJumping()) { if (playerController.IsControlledDescent()) { this.anim.CrossFade("jetpackjump", 0.2f); } else { if (playerController.HasJumpReachedApex()) { this.anim.CrossFade("jumpfall", 0.2f); } else { this.anim.CrossFade("jump", 0.2f); } } } else { if (!playerController.IsGroundedWithTimeout()) { this.anim.CrossFade("ledgefall", 0.2f); } else { this.anim.Blend("ledgefall", 0f, 0.2f); } } }
public virtual IEnumerator Start() { ThirdPersonController playerController = (ThirdPersonController)this.GetComponent(typeof(ThirdPersonController)); this.GetComponent <AudioSource>().loop = false; this.GetComponent <AudioSource>().Stop(); var particles = this.GetComponentsInChildren(typeof(ParticleSystem)); Light childLight = (Light)this.GetComponentInChildren(typeof(Light)); foreach (ParticleSystem p in particles) { var e = p.emission; e.enabled = false; } childLight.enabled = false; while (true) { bool isFlying = playerController.IsJumping(); if (isFlying) { if (!this.GetComponent <AudioSource>().isPlaying) { this.GetComponent <AudioSource>().Play(); } } else { this.GetComponent <AudioSource>().Stop(); } foreach (ParticleSystem p in particles) { var e = p.emission; e.enabled = isFlying; } if (isFlying) { this.litAmount = Mathf.Clamp01(this.litAmount + (Time.deltaTime * 2)); } else { this.litAmount = Mathf.Clamp01(this.litAmount - (Time.deltaTime * 2)); } childLight.enabled = isFlying; childLight.intensity = this.litAmount; yield return(null); } }
private void LateUpdate() { var targetCenter = Target.position + _centerOffset; var targetHead = Target.position + _headOffset; // When jumping don't move the camera upwards but only down! if (_controller.IsJumping()) { // We'd be moving the camera upwards, do that only if it's really high! float newTargetHeight = targetCenter.y + Height; if (newTargetHeight < _targetHeight || newTargetHeight - _targetHeight > 5) { _targetHeight = targetCenter.y + Height; } } // When walking always update the height else { _targetHeight = targetCenter.y + Height; } // We start snapping when user pressed Fire2! _isSnapping = Input.GetButton("Fire2"); if (_isSnapping) { //ApplySnapping(targetCenter); ApplyPositionDamping(new Vector3(targetCenter.x, _targetHeight, targetCenter.z)); //TODO: Optimize!! Transform t = GameObject.Find("Player").GetComponent <ThirdPersonTargetting>().GetCurrentTarget(); //Set our target as the snapped object targetCenter = t.position; } else { ApplyPositionDamping(new Vector3(targetCenter.x, _targetHeight, targetCenter.z)); } SetupRotation(targetCenter, targetHead); }
void Update() { if (characterController.IsJumping() && !isFlying || characterController.IsFalling() ) { //next jump button shouldn't be too close to first jump time if ((characterController.LastJumpTime() + jumpOffset) > Time.time ) return; if (Input.GetButtonDown("Jump")) ActivateFlying(); } if (isFlying) FlyingUpdate(); }
void Apply(Transform dummyTarget, Vector3 dummyCenter) { Vector3 targetCenter = target.position + centerOffset; Vector3 targetHead = target.position + headOffset; // When jumping don't move camera upwards but only down! if (controller.IsJumping()) { // We'd be moving the camera upwards, do that only if it's really high float newTargetHeight = targetCenter.y + height; if (newTargetHeight < targetHeight || newTargetHeight - targetHeight > 5) { targetHeight = targetCenter.y + height; } // When walking always update the target height } else { targetHeight = targetCenter.y + height; } // We start snapping when user pressed Fire2! if (Input.GetButton("Fire2") && !isSnapping) { velocity = Vector3.zero; isSnapping = true; } if (isSnapping) { ApplySnapping(targetCenter); } else { ApplyPositionDamping(new Vector3(targetCenter.x, targetHeight, targetCenter.z)); } SetUpRotation(targetCenter, targetHead); }
void Apply(Transform dummyTarget, Vector3 dummyCenter) { // Early out if we don't have a target if (!controller) { return; } Vector3 targetCenter = _target.position + centerOffset; Vector3 targetHead = _target.position + headOffset; // DebugDrawStuff(); // Calculate the current & target rotation angles float originalTargetAngle = _target.eulerAngles.y; float currentAngle = cameraTransform.eulerAngles.y; // Adjust real target angle when camera is locked float targetAngle = originalTargetAngle; // When pressing Fire2 (alt) the camera will snap to the target direction real quick. // It will stop snapping when it reaches the target if (Input.GetButton("Fire2")) { snap = true; } if (snap) { // We are close to the target, so we can stop snapping now! if (AngleDistance(currentAngle, originalTargetAngle) < 3.0f) { snap = false; } currentAngle = Mathf.SmoothDampAngle(currentAngle, targetAngle, ref angleVelocity, snapSmoothLag, snapMaxSpeed); } // Normal camera motion else { if (controller.GetLockCameraTimer() < lockCameraTimeout) { targetAngle = currentAngle; } // Lock the camera when moving backwards! // * It is really confusing to do 180 degree spins when turning around. if (AngleDistance(currentAngle, targetAngle) > 160 && controller.IsMovingBackwards()) { targetAngle += 180; } currentAngle = Mathf.SmoothDampAngle(currentAngle, targetAngle, ref angleVelocity, angularSmoothLag, angularMaxSpeed); } // When jumping don't move camera upwards but only down! if (controller.IsJumping()) { // We'd be moving the camera upwards, do that only if it's really high float newTargetHeight = targetCenter.y + height; if (newTargetHeight < targetHeight || newTargetHeight - targetHeight > 5) { targetHeight = targetCenter.y + height; } } // When walking always update the target height else { targetHeight = targetCenter.y + height; } // Damp the height float currentHeight = cameraTransform.position.y; currentHeight = Mathf.SmoothDamp(currentHeight, targetHeight, ref heightVelocity, heightSmoothLag); // Convert the angle into a rotation, by which we then reposition the camera Quaternion currentRotation = Quaternion.Euler(0, currentAngle, 0); // Set the position of the camera on the x-z plane to: // distance meters behind the target cameraTransform.position = targetCenter; cameraTransform.position += currentRotation * Vector3.back * distance; Vector3 tempCameraTransformPos = cameraTransform.position; tempCameraTransformPos.y = currentHeight; cameraTransform.position = tempCameraTransformPos; // Always look at the target SetUpRotation(targetCenter, targetHead); }
private void GroundedAnimations() { //blend off any residual animations animationGameObject.animation.Blend("fall", 0.0f, 0.1f); animationGameObject.animation.Blend("jump", 0.0f, 0.1f); animationGameObject.animation.Blend("fly", 0.0f, 0.1f); animationGameObject.animation.Blend("elevate", 0.0f, 0.1f); animationGameObject.animation.Blend("lower", 0.0f, 0.1f); //Fade in Run if (characterController.GetSpeed() >= (characterController.GetRunSpeed() - 1)) { animationGameObject.animation.CrossFade("run"); currentStatus = AnimationStatus.Run; //fade out walk animationGameObject.animation.Blend("walk", 0.0f, 0.3f); } // Fade in walk else if (characterController.GetSpeed() > 0.1f) { if (characterController.AreMovingKeysDown()) { animationGameObject.animation.CrossFade("walk"); currentStatus = AnimationStatus.Walk; } else { animationGameObject.animation.Blend("walk", 0.0f, 0.1f); } // We fade out jumpland realy quick otherwise we get sliding feet animationGameObject.animation.Blend("run", 0.0f, 0.3f); } // Fade out walk and run so just idle remains else { animationGameObject.animation.Blend("walk", 0.0f, 0.3f); currentStatus = AnimationStatus.Idle; // animationGameObject.animation.Blend("run", 0.0f, 0.3f); } animationGameObject.animation["run"].normalizedSpeed = runSpeedScale; animationGameObject.animation["walk"].normalizedSpeed = walkSpeedScale; //is Character Jumping if (characterController.IsJumping()) { if (!characterController.HasJumpReachedApex() && characterController.IsJumping()) { animationGameObject.animation.CrossFade("jump", 0.2f); animationGameObject.animation.Blend("fall", 0.0f, 0.3f); } else { animationGameObject.animation.Blend("jump", 0.0f, 0.3f); animationGameObject.animation.CrossFade("fall", 0.2f); } currentStatus = AnimationStatus.Jump; animationGameObject.animation.Blend("walk", 0.0f, 0.1f); animationGameObject.animation.Blend("run", 0.0f, 0.1f); } }
void Update() { ThirdPersonController marioController = GetComponent <ThirdPersonController> (); float currentSpeed = marioController.GetSpeed(); networkSyncAnimation = GetComponent <NetworkSyncAnimation> (); // Fade in run if (currentSpeed > marioController.walkSpeed) { animation.CrossFade("run"); // We fade out jumpland quick otherwise we get sliding feet animation.Blend("jumpland", 0); networkSyncAnimation.SendMessage("SyncAnimation", "run"); // Fade in walk } else if (currentSpeed > 0.1f) { animation.CrossFade("walk"); // We fade out jumpland realy quick otherwise we get sliding feet animation.Blend("jumpland", 0); networkSyncAnimation.SendMessage("SyncAnimation", "walk"); // Fade out walk and run } else { animation.CrossFade("idle"); networkSyncAnimation.SendMessage("SyncAnimation", "idle"); } animation["run"].normalizedSpeed = runSpeedScale; animation["walk"].normalizedSpeed = walkSpeedScale; if (marioController.IsJumping()) { if (marioController.IsCapeFlying()) { animation.CrossFade("jetpackjump", 0.2f); networkSyncAnimation.SendMessage("SyncAnimation", "jetpackjump"); } else if (marioController.HasJumpReachedApex()) { animation.CrossFade("jumpfall", 0.2f); networkSyncAnimation.SendMessage("SyncAnimation", "jumpfall"); } else { animation.CrossFade("jump", 0.2f); networkSyncAnimation.SendMessage("SyncAnimation", "jump"); } // We fell down somewhere } else if (!marioController.IsGroundedWithTimeout()) { animation.CrossFade("ledgefall", 0.2f); networkSyncAnimation.SendMessage("SyncAnimation", "ledgefall"); // We are not falling down anymore } else { animation.Blend("ledgefall", 0.0f, 0.2f); } }
void Apply(Transform dummyTarget, Vector3 dummyCenter) { // Early out if we don't have a target if (!controller) { return; } bool needGoOn = false; Vector3 targetCenter = _target.position + centerOffset; Vector3 targetHead = _target.position + headOffset; var strength = Input.GetAxis("Mouse ScrollWheel"); if (strength != 0) { distance -= strength * mSpeed; distance = Mathf.Clamp(distance, minDistance, maxDistance); /* * if(distance <= 1) * { * SetVisible(false); * minAngleY = -80; * } * else if(firstPersonLook) * { * SetVisible(true); * } * else if(distance < look_height) * { * minAngleY = (distance - 2) * (yTopLimit - yMinLimit)/(look_height - 2) - yTopLimit; * minAngleY = - minAngleY; * } * else * { * minAngleY = yMinLimit; * } */ needGoOn = true; } var originalTargetAngle = 360 - _target.eulerAngles.y; current_hor_angle = 360 - cameraTransform.eulerAngles.y; if (!snapped) { var targetAngle = originalTargetAngle; float dis_angle = 0; if (dosnap) { dis_angle = AngleDistance(360 - current_hor_angle, originalTargetAngle); current_hor_angle = Mathf.SmoothDampAngle(current_hor_angle, targetAngle, ref angleVelocity, snapSmoothLag, snapMaxSpeed); } // We are close to the target, so we can stop snapping now! dis_angle = 0; if (dis_angle <= 10) { snapped = true; dosnap = false; } else if (dis_angle < 3) { dosnap = false; } if (!snapped && !dosnap) { current_hor_angle = Mathf.SmoothDampAngle(current_hor_angle, targetAngle, ref angleVelocity, angularSmoothLag, angularMaxSpeed); } needGoOn = true; } else { float rotation_h = 0; float rotation_v = 0; if (Input.GetMouseButton(1)) { rotation_h = -Input.GetAxis("Mouse X") * xSpeed * 0.02f; rotation_v = -Input.GetAxis("Mouse Y") * ySpeed * 0.02f; } needGoOn = needGoOn || (rotation_h != 0 || rotation_v != 0); current_hor_angle += rotation_h; current_hor_angle = Mathf.Repeat(current_hor_angle, 360); current_ver_angle += rotation_v; current_ver_angle = Mathf.Clamp(current_ver_angle, minAngleY, yMaxLimit); } needGoOn = needGoOn || controller.IsMoving(); needGoOn = needGoOn || controller.IsJumping(); needGoOn = needGoOn || needRefreshCameraPos; needRefreshCameraPos = false; //if (!needGoOn)/*没有鼠标键盘事件,返回即可,相机一般不会自动更新。除非未来有其他情形,那时候再添加*/ //{ // MouseMoveContr mousecl = GetComponent<MouseMoveContr>(); // bool mouseMoveFlag = mousecl.GetMouseMoveFlag(); // if (!mouseMoveFlag) // { // return; // } //} float rad_angle_h = (current_hor_angle - 90.0f) * Mathf.Deg2Rad; var rad_angle_v = current_ver_angle * Mathf.Deg2Rad; var camera_pos = Vector3.zero; var radius_hor = distance * Mathf.Cos(rad_angle_v); float slope = -Mathf.Sin(rad_angle_v); camera_pos.x = radius_hor * Mathf.Cos(rad_angle_h) + targetHead.x;/*计算相机位置是用 头部为球中心计算的*/ camera_pos.z = radius_hor * Mathf.Sin(rad_angle_h) + targetHead.z; camera_pos.y = -distance * slope + targetHead.y; if (camera_pos.y < targetHead.y - look_height) { camera_pos.y = targetHead.y - look_height; } RaycastHit hit; bool modified = false; float hor_dis = 0.0f; if (camera_pos.y < targetCenter.y) { var testPt = camera_pos; testPt.y = targetCenter.y; if (Physics.Raycast(testPt, Vector3.down, out hit, 50)) /*这个检测必须进行,不能完全指望后面的检测,否则会有微小的显示问题。一般发生在摄像机贴近地面跑动时*/ { if (camera_pos.y < hit.point.y + 0.5) /*偏移0.5.防止过于接近地面,并且在地面上面的情况,会因为摄像机近截面问题。导致显示地下的内容*/ { //modified = true; hor_dis = Vector3.Distance(targetCenter, new Vector3(camera_pos.x, targetCenter.y, camera_pos.z)); camera_pos = hit.point; camera_pos.y = (slope > 0.95f) ? hit.point.y : (camera_pos.y + hor_dis / maxDistance); //摄像头在脚下的时候,hor_dis几乎为0 modified = false; //Debug.Log("hit down.....camera_pos : " +camera_pos); } } } //if (modified) //{ // hor_dis = Vector3.Distance(targetCenter, new Vector3(camera_pos.x, targetCenter.y, camera_pos.z)); // camera_pos = hit.point; // camera_pos.y = (slope > 0.95f) ? hit.point.y : (camera_pos.y + hor_dis / maxDistance); // //摄像头在脚下的时候,hor_dis几乎为0 // modified = false; // //Debug.Log("hit down.....camera_pos : " +camera_pos); //} var real_dis = Vector3.Distance(targetCenter, camera_pos); var direction = camera_pos - targetCenter; if (Physics.Raycast(targetCenter, direction, out hit, real_dis) && hit.collider.gameObject != gameObject) { // modified = false; // if(hit.collider.bounds.size.magnitude <= 15) { // modified = false; // } else if (hit.collider.gameObject.tag == "bridge") { // camera_pos.y = camera_pos.y + 2.5; // } else if (hit.collider.gameObject.tag == "through"){ // modified = false; // } else { // modified = true; // } // Debug.LogError(hit.point.y < targetHead.y); camera_pos = hit.point; if (hit.point.y < targetHead.y) { camera_pos.y = targetHead.y; // Debug.LogError(camera_pos); } } // // if(modified) // { // hor_dis = Vector3.Distance(targetCenter,Vector3(camera_pos.x,targetCenter.y,camera_pos.z)); // camera_pos = hit.point; // camera_pos.y = (slope > 0.95)?hit.point.y:(camera_pos.y + hor_dis/maxDistance);/*摄像头在脚下的时候,hor_dis几乎为0*/ // } cameraTransform.position = camera_pos; var offsetToCenter = targetHead - cameraTransform.position; cameraTransform.rotation = Quaternion.LookRotation(new Vector3(offsetToCenter.x, offsetToCenter.y, offsetToCenter.z)); Debug.DrawLine(targetHead, camera_pos, Color.red); }