// Update is called once per frame void Update() { // Get the input vector from kayboard or analog stick Vector3 directionVector = new Vector3(InputDB.GetAxis("Horizontal"), 0, InputDB.GetAxis("Vertical")); 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 float 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; } // Apply the direction to the CharacterMotorDB motor.inputMoveDirection = transform.rotation * directionVector; motor.inputJump = InputDB.GetButton("Jump"); }
void FixedUpdate() { if (grounded && PlayerWeapons.canMove) { // We are grounded, so recalculate movedirection directly from axes moveDirection = new Vector3(InputDB.GetAxis("Horizontal"), 0, InputDB.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed; if (InputDB.GetButton("Jump")) { moveDirection.y = jumpSpeed; if (crouching) { stopCrouching = true; NormalSpeed(); } } } // Apply gravity moveDirection.y -= gravity * Time.deltaTime; // Move the controller CharacterController controller = GetComponent <CharacterController>(); CollisionFlags flags = controller.Move(moveDirection * Time.deltaTime); grounded = (flags & CollisionFlags.CollidedBelow) != 0; if ((Mathf.Abs(moveDirection.x) > 0) && grounded || (Mathf.Abs(moveDirection.z) > 0 && grounded)) { if (!walking) { walking = true; BroadcastMessage("Walking", SendMessageOptions.DontRequireReceiver); } } else if (walking) { walking = false; stopWalkingTime = Time.time; BroadcastMessage("StopWalking", SendMessageOptions.DontRequireReceiver); } }
void Update() { if (player == null) { return; } if (ladder) { if (Input.GetButtonDown("Jump")) { ReleasePlayer(); } float lastVal = val; val += InputDB.GetAxis("Vertical") * sensitivity * Time.deltaTime; val = Mathf.Clamp01(val); soundAmt -= Mathf.Abs(lastVal - val); if (soundAmt <= 0) { soundAmt = soundInterval; GetComponent <AudioSource>().Play(); } // player.position.y = Mathf.Lerp(transform.position.y, exitPos.position.y, val); player.position = new Vector3(player.position.x, Mathf.Lerp(transform.position.y, exitPos.position.y, val), player.position.z); if (val == 1 && !GunScript.takingOut && !GunScript.puttingAway) { ExitLadder(); } if (val < minHeight && ((lastVal > val) || val == 0) && !GunScript.takingOut && !GunScript.puttingAway) { ReleasePlayer(); } } }
void Update() { if (freeze || !PlayerWeapons.playerActive) { return; } if (retSensitivity > 0) { retSensitivity *= -1; } if (useLookMotion && PlayerWeapons.canLook) { Quaternion xQuaternion; Quaternion yQuaternion; // Read the mouse input axis rotationX += InputDB.GetAxis("Mouse X") * sensitivityX; rotationY += InputDB.GetAxis("Mouse Y") * sensitivityY; rotationZ += InputDB.GetAxis("Mouse X") * sensitivityZ; rotationX = ClampAngle(rotationX, minimumX, maximumX); rotationY = ClampAngle(rotationY, minimumY, maximumY); rotationZ = ClampAngle(rotationZ, -actualZRange, actualZRange); if (Mathf.Abs(Input.GetAxis("Mouse X")) < .05) { if (sensitivityX > 0) { rotationX -= rotationX * Time.deltaTime * retSensitivity * 7; rotationZ -= rotationZ * Time.deltaTime * retSensitivity * 7; rotationY += rotationY * Time.deltaTime * retSensitivity * 7; } else { rotationX += rotationX * Time.deltaTime * retSensitivity * 7; rotationZ += rotationZ * Time.deltaTime * retSensitivity * 7; rotationY += rotationY * Time.deltaTime * retSensitivity * 7; } } xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up); Quaternion zQuaternion = Quaternion.AngleAxis(rotationZ, Vector3.forward); yQuaternion = Quaternion.AngleAxis(rotationY, Vector3.left); transform.localRotation = Quaternion.Lerp(transform.localRotation, originalRotation * xQuaternion * yQuaternion * zQuaternion, Time.deltaTime * 10); } if (useWalkMotion) { //Velocity-based changes Vector3 relVelocity = transform.InverseTransformDirection(PlayerWeapons.CM.movement.velocity); float zVal; float xVal; float xVal2; lastOffset = posOffset; float s = new Vector3(PlayerWeapons.CM.movement.velocity.x, 0, PlayerWeapons.CM.movement.velocity.z).magnitude / 14; if (!AimMode.staticAiming) { float xPos = Mathf.Clamp(relVelocity.x * xPosMoveSensitivity, -xPosMoveRange * s, xPosMoveRange * s); posOffset.x = Mathf.Lerp(posOffset.x, xPos, Time.deltaTime * xPosAdjustSpeed); // + startPos.x; float zPos = Mathf.Clamp(relVelocity.z * zPosMoveSensitivity, -zPosMoveRange * s, zPosMoveRange * s); posOffset.z = Mathf.Lerp(posOffset.z, zPos, Time.deltaTime * zPosAdjustSpeed); // + startPos.z; } else { posOffset.x = Mathf.Lerp(posOffset.x, 0, Time.deltaTime * xPosAdjustSpeed * 3); // + startPos.x; posOffset.z = Mathf.Lerp(posOffset.z, 0, Time.deltaTime * zPosAdjustSpeed * 3); // + startPos.z; } //Apply Jostle lastJostle = curJostle; curJostle = Vector3.Lerp(curJostle, jostleAmt, Time.deltaTime * 10); jostleAmt = Vector3.Lerp(jostleAmt, Vector3.zero, Time.deltaTime * 3); lastTarget = curTarget; curTarget = Vector3.Lerp(curTarget, posOffset, Time.deltaTime * 8); transform.localPosition += curTarget - lastTarget; transform.localPosition += curJostle - lastJostle; } }
void Update() { if (freeze || !PlayerWeapons.canLook || individualFreeze) { return; } Quaternion xQuaternion; Quaternion yQuaternion; float offsetVal; if (axes == RotationAxes.MouseX) { rotationX += InputDB.GetAxis("Mouse X") * sensitivityX; float xDecrease; if (totalOffsetX > 0) { xDecrease = Mathf.Clamp(resetSpeed * Time.deltaTime, 0, totalOffsetX); } else { xDecrease = Mathf.Clamp(resetSpeed * -Time.deltaTime, totalOffsetX, 0); } if (resetDelay > 0) { xDecrease = 0; resetDelay = Mathf.Clamp(resetDelay - Time.deltaTime, 0, resetDelay); } if (Random.value < .5) { offsetX *= -1; } if ((totalOffsetX < maxKickback && totalOffsetX >= 0) || (totalOffsetX > -maxKickback && totalOffsetX <= 0)) { totalOffsetX += offsetX; } else { //offsetX = 0; resetDelay *= .5f; } rotationX = ClampAngle(rotationX, minimumX, maximumX) + offsetX - xDecrease; if ((Input.GetAxis("Mouse X") * sensitivityX) < 0) { totalOffsetX += Input.GetAxis("Mouse X") * sensitivityX; } rotationX += Mathf.Sin(Time.time) * idleSway; totalOffsetX -= xDecrease; if (totalOffsetX < 0) { totalOffsetX = 0; } xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up); tRotation = originalRotation * xQuaternion; offsetVal = Mathf.Clamp(totalOffsetX * smoothFactor, 1, smoothFactor); if (smooth) { transform.localRotation = Quaternion.Slerp(transform.localRotation, tRotation, Time.deltaTime * 25 / smoothFactor * offsetVal); } else { transform.localRotation = tRotation; } } else { rotationY += InputDB.GetAxis("Mouse Y") * sensitivityY; float yDecrease = Mathf.Clamp(resetSpeed * Time.deltaTime, 0, totalOffsetY); if (resetDelay > 0) { yDecrease = 0; resetDelay = Mathf.Clamp(resetDelay - Time.deltaTime, 0, resetDelay); } if (totalOffsetY < maxKickback) { totalOffsetY += offsetY; } else { offsetY = 0; resetDelay *= .5f; } rotationY = ClampAngle(rotationY, minimumY, maximumY) + offsetY - yDecrease; if ((Input.GetAxis("Mouse Y") * sensitivityY) < 0) { totalOffsetY += Input.GetAxis("Mouse Y") * sensitivityY; } rotationY += Mathf.Sin(Time.time) * idleSway; totalOffsetY -= yDecrease; if (totalOffsetY < 0) { totalOffsetY = 0; } yQuaternion = Quaternion.AngleAxis(rotationY, Vector3.left); tRotation = originalRotation * yQuaternion; offsetVal = Mathf.Clamp(totalOffsetY * smoothFactor, 1, smoothFactor); if (smooth) { transform.localEulerAngles = new Vector3(Quaternion.Slerp(transform.localRotation, tRotation, Time.deltaTime * 25 / smoothFactor * offsetVal).eulerAngles.x, transform.localEulerAngles.y, transform.localEulerAngles.z); } else { transform.localEulerAngles = new Vector3(tRotation.x, transform.localEulerAngles.y, transform.localEulerAngles.z); ; } } offsetY = 0; offsetX = 0; }