void Update() { if (isJoycon) { if (m_joycons == null || m_joycons.Count <= 0) { return; } //var accelR = m_joyconR.GetAccel(); // 加速度 //Debug.Log("accelR" + accelR); var accelL = m_joyconL.GetAccel(); // 加速度 Debug.Log("accelL" + accelL); //var accelAve = (accelR.magnitude + accelL.magnitude ) / 2.0f; float accelMag = accelL.magnitude; speed = accelMag * mulSpeed; accelMag /= 3.0f; Debug.Log("accelMag" + accelMag); if (ground) { //Debug.Log(accelMag); if (accelMag > 0.4) { float x = Time.deltaTime * speed; float z = Time.deltaTime * speed; rb.MovePosition(transform.position + new Vector3(0, 0, z)); Vector3 direction = transform.position - playerPos; Debug.Log(direction.magnitude); if (direction.magnitude > 0.01f) { transform.rotation = Quaternion.LookRotation(new Vector3 (0, 0, direction.z)); animator.SetBool("Running", true); animator.speed = accelMag; } else { //animator.SetBool("Running", false); } playerPos = transform.position; } else { animator.SetBool("Running", false); } } } else { //地面に接触していると作動する if (ground) { //A・Dキー、←→キーで横移動 float x = Input.GetAxisRaw("Horizontal") * Time.deltaTime * speed; //W・Sキー、↑↓キーで前後移動 float z = Input.GetAxisRaw("Vertical") * Time.deltaTime * speed; //現在の位置+入力した数値の場所に移動する rb.MovePosition(transform.position + new Vector3(x, 0, z)); //ユニティちゃんの最新の位置から少し前の位置を引いて方向を割り出す Vector3 direction = transform.position - playerPos; //移動距離が少しでもあった場合に方向転換 if (direction.magnitude > 0.01f) { //directionのX軸とZ軸の方向を向かせる transform.rotation = Quaternion.LookRotation(new Vector3 (direction.x, 0, direction.z)); //走るアニメーションを再生 animator.SetBool("Running", true); } else { //ベクトルの長さがない=移動していない時は走るアニメーションはオフ animator.SetBool("Running", false); } //ユニティちゃんの位置を更新する playerPos = transform.position; //スペースキーやゲームパッドの3ボタンでジャンプ if (Input.GetButton("Jump")) { //thrustの分だけ上方に力がかかる rb.AddForce(transform.up * thrust); //速度が出ていたら前方と上方に力がかかる if (rb.velocity.magnitude > 0) { rb.AddForce(transform.forward * thrust + transform.up * thrust); } } } } }
public Vector3 PollJoyconForTilt() { PollJoycon(); return(CONTROLLER.GetAccel()); }
/** Handles both controller and keyboard inputs for gravity switching. * Checks our current gravity against the player's desired gravity, and, * if we want to switch gravity, then call the code to do so. */ void Update() { print(currGrav); //don't move if dead if (deathCheck.isDead) { return; } if (rb.velocity.magnitude > maxFallSpeed) { rb.velocity = Vector2.ClampMagnitude(rb.velocity, maxFallSpeed); } #if UNITY_STANDALONE || UNITY_EDITOR if (j != null) { accel = j.GetAccel(); GravityCheck(); } #endif if (!rotLevel) { // Handle keyboard inputs for switching the gravity of the player. if (Input.GetKeyDown(tiltUp) && currGrav != GRAVITY.UP) { // Set Gravity to Up. if (!rotLevel) { GravitySwitch(GRAVITY.UP); } else { int test = ((int)currGrav + 2) % 4; Debug.Log(test); GravitySwitch((GRAVITY)test); } } else if (Input.GetKeyDown(tiltDown) && currGrav != GRAVITY.DOWN) { // Set Gravity to Down. if (!rotLevel) { GravitySwitch(GRAVITY.DOWN); } //transform.Rotate(new Vector3(0, 0, 1) * rotationSpeed * Time.deltaTime, Space.World); } else if (Input.GetKeyDown(tiltLeft) && currGrav != GRAVITY.LEFT) { // Set Gravity to Left. if (!rotLevel) { GravitySwitch(GRAVITY.LEFT); } else { int test = ((int)currGrav - 1) % 4; Debug.Log(test); GravitySwitch((GRAVITY)test); } } else if (Input.GetKeyDown(tiltRight) && currGrav != GRAVITY.RIGHT) { // Set Gravity to Right. if (!rotLevel) { GravitySwitch(GRAVITY.RIGHT); } else { int test = ((int)currGrav + 1) % 4; Debug.Log(test); GravitySwitch((GRAVITY)test); } } } else { // Handle keyboard inputs for switching the gravity of the player. if (Input.GetKeyDown(tiltUp)) { // Set Gravity to Up. if (!rotLevel) { GravitySwitch(GRAVITY.UP); } else { int newGrav = ((int)currGrav + 2) % 4; Debug.Log(newGrav); GravitySwitch((GRAVITY)newGrav); } } else if (Input.GetKeyDown(tiltDown) && currGrav != GRAVITY.DOWN) { // Set Gravity to Down. if (!rotLevel) { GravitySwitch(GRAVITY.DOWN); } //transform.Rotate(new Vector3(0, 0, 1) * rotationSpeed * Time.deltaTime, Space.World); } else if (Input.GetKeyDown(tiltLeft)) { // Set Gravity to Left. if (!rotLevel) { GravitySwitch(GRAVITY.LEFT); } else { int newGrav = ((int)currGrav - 1); if (newGrav == -1) { newGrav = 3; } GravitySwitch((GRAVITY)newGrav); } } else if (Input.GetKeyDown(tiltRight)) { // Set Gravity to Right. if (!rotLevel) { GravitySwitch(GRAVITY.RIGHT); } else { int newGrav = ((int)currGrav + 1) % 4; Debug.Log(newGrav); GravitySwitch((GRAVITY)newGrav); } } } if (!rotLevel) { //handles the rotation of the character when the y gravity is manipulated if (currGrav == GRAVITY.UP && (Vector3.Distance(transform.eulerAngles, new Vector3(0.0f, 0.0f, 180.0f)) > 0.01f)) { transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, new Vector3(0.0f, 0.0f, 180.0f), Time.deltaTime * rotationSpeed); } else if (currGrav == GRAVITY.DOWN && (Vector3.Distance(transform.eulerAngles, Vector3.zero) > 0.01f)) { transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, Vector3.zero, Time.deltaTime * rotationSpeed); } else if (currGrav == GRAVITY.RIGHT && (Vector3.Distance(transform.eulerAngles, new Vector3(0.0f, 0.0f, 90.0f)) > 0.01f)) { transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, new Vector3(0.0f, 0.0f, 90.0f), Time.deltaTime * rotationSpeed); } else if (currGrav == GRAVITY.LEFT && (Vector3.Distance(transform.eulerAngles, new Vector3(0.0f, 0.0f, -90.0f)) > 0.01f)) { transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, new Vector3(0.0f, 0.0f, -90.0f), Time.deltaTime * rotationSpeed); } } else { levelRot.Rotate((int)currGrav); } currentPos = transform.position; // Keyboard inputs for player movement if (Input.GetKey(KeyCode.A)) { Movement(MOVE.LEFT); anim.SetBool("IsFloating", true); gameObject.GetComponent <SpriteRenderer>().flipX = true; } else if (Input.GetKey(KeyCode.D)) { Movement(MOVE.RIGHT); anim.SetBool("IsFloating", true); gameObject.GetComponent <SpriteRenderer>().flipX = false; } else { anim.SetBool("IsFloating", false); } // switch stick controls #if UNITY_STANDALONE || UNITY_EDITOR if (j != null) { jStick = j.GetStick(); // jStick[0] is x position of stick, jStick[1] is y position if (currGrav.Equals(GRAVITY.DOWN)) { if (jStick[1] > 0.0f) // joystick is to the left { Movement(MOVE.LEFT); } else if (jStick[1] < 0.0f) // joystick is to the right { Movement(MOVE.RIGHT); } } else if (currGrav.Equals(GRAVITY.UP)) { if (jStick[1] < 0.0f) // joystick is to the left { Movement(MOVE.LEFT); } else if (jStick[1] > 0.0f) // joystick is to the right { Movement(MOVE.RIGHT); } } else if (currGrav.Equals(GRAVITY.RIGHT)) { if (jStick[0] < 0.0f) // joystick is to the left { Movement(MOVE.LEFT); } else if (jStick[0] > 0.0f) // joystick is to the right { Movement(MOVE.RIGHT); } } else if (currGrav.Equals(GRAVITY.LEFT)) { if (jStick[0] > 0.0f) // joystick is to the left { Movement(MOVE.LEFT); } else if (jStick[0] < 0.0f) // joystick is to the right { Movement(MOVE.RIGHT); } } } #endif }
// Update is called once per frame void Update() { if (GameFlowManager.Instance.GameOver) { return; } #if !KEYBOARD_MODE // make sure the Joycon only gets checked if attached if (joycons.Count > 0) { Joycon j = joycons[jc_ind]; // GetButtonDown checks if a button has been pressed (not held) //if (j.GetButtonDown(Joycon.Button.SHOULDER_2)) //{ // Debug.Log("Shoulder button 2 pressed"); // // GetStick returns a 2-element vector with x/y joystick components // Debug.Log(string.Format("Stick x: {0:N} Stick y: {1:N}", j.GetStick()[0], j.GetStick()[1])); // // Joycon has no magnetometer, so it cannot accurately determine its yaw value. Joycon.Recenter allows the user to reset the yaw value. // j.Recenter(); //} //if (j.GetButtonDown(Joycon.Button.DPAD_DOWN)) //{ // Debug.Log("Rumble"); // // Rumble for 200 milliseconds, with low frequency rumble at 160 Hz and high frequency rumble at 320 Hz. For more information check: // // https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/rumble_data_table.md // j.SetRumble(160, 320, 0.6f, 200); // // The last argument (time) in SetRumble is optional. Call it with three arguments to turn it on without telling it when to turn off. // // (Useful for dynamically changing rumble values.) // // Then call SetRumble(0,0,0) when you want to turn it off. //} //if (j.GetButtonDown(Joycon.Button.DPAD_LEFT)) //{ // Debug.Log("Rumble"); // // Rumble for 200 milliseconds, with low frequency rumble at 160 Hz and high frequency rumble at 320 Hz. For more information check: // // https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/rumble_data_table.md // int low_freq = Random.Range(100, 150); // int high_freq = Random.Range(320, 500); // float amp = Random.Range(0.5f, 1f); // int time = Random.Range(100, 500); // j.SetRumble(low_freq, high_freq, amp, time); // // The last argument (time) in SetRumble is optional. Call it with three arguments to turn it on without telling it when to turn off. // // (Useful for dynamically changing rumble values.) // // Then call SetRumble(0,0,0) when you want to turn it off. //} stick = j.GetStick(); // Gyro values: x, y, z axis values (in radians per second) gyro = j.GetGyro(); // Accel values: x, y, z axis values (in Gs) oldAccel = accel; accel = j.GetAccel(); orientation = j.GetVector(); var position = transform.position; float sensitivity = this.sensitivity / 1000; position.x += stick[0] * sensitivity; position.z += stick[1] * sensitivity; ClampPosition(ref position); hover1.UpdateRotation = false; hover2.UpdateRotation = false; if (handState == HandState.Idle) { if (j.GetButtonDown(Joycon.Button.SHOULDER_2)) { holdingButton = Joycon.Button.SHOULDER_2; startPressTime = Time.time; } else if (j.GetButtonDown(Joycon.Button.STICK)) { holdingButton = Joycon.Button.STICK; startPressTime = Time.time; } else if (holdingButton != Joycon.Button.CAPTURE) { if (j.GetButtonUp(holdingButton)) { holdingButton = Joycon.Button.CAPTURE; handState = HandState.GoingDown; startPressTime = 0; } else if (holdingSomethingYeetable && Time.time - startPressTime > holdDurationRequirement) { handState = HandState.ChargingYeet; startPressTime = 0; } } //if (j.GetButton(Joycon.Button.SHOULDER_1)) //{ // handState = HandState.GoingDown; //} //else if (holdingSomethingYeetable && j.GetButton(Joycon.Button.SHOULDER_2)) //{ // handState = HandState.ChargingYeet; //} } if (handState == HandState.ChargingYeet) { if (j.GetButton(holdingButton)) { yeetingPower += (oldAccel - accel).magnitude; yeetingPower = Mathf.Clamp(yeetingPower, 0, MAX_YEETING_POWER); var yeetNormalized = Mathf.Clamp01(yeetingPower / MAX_YEETING_POWER); var yoteMax = Mathf.Lerp(80, 320, yeetNormalized); var yoteMin = Mathf.Lerp(80, 160, yeetNormalized); j.SetRumble(yoteMin, yoteMax, 0.6f); } else if (j.GetButtonUp(holdingButton)) { holdingButton = Joycon.Button.CAPTURE; //Debug.LogFormat("Yoting {0}", yeetingPower); handState = HandState.Yeeting; var yeetNormalized = Mathf.Clamp01(yeetingPower / MAX_YEETING_POWER); var yoteMax = Mathf.Lerp(80, 320, yeetNormalized); var yoteMin = Mathf.Lerp(80, 160, yeetNormalized); j.SetRumble(yoteMin, yoteMax, 0.6f, 200); var direction = ourPosition.position.x <= opponentPosition.position.x; if (yeetDirection != direction) { yeetRotMin *= -1; yeetRotMax *= -1; } yeetDirection = direction; } } if (handState == HandState.GoingDown) { position.y -= descentSpeed * Time.deltaTime; var grabbyPointPosition = grabbyPoint.localPosition; grabbyPointPosition.y -= descentSpeed * grabbySpeedIncrement * Time.deltaTime; grabbyPoint.localPosition = grabbyPointPosition; var positionTraveled = Mathf.InverseLerp(MaxDescentPosition, OriginalYPosition, position.y); materialOffset.y = GetTextureOffsetModifier(positionTraveled); //Debug.LogFormat("GoingDown: Position Traveled = {0} => Mat offset = {1}", positionTraveled, materialOffset.y); material.mainTextureOffset = materialOffset; if (position.y <= MaxDescentPosition) { position.y = MaxDescentPosition; DetectObjectCollision(); handState = HandState.GoingUp; materialOffset.y = 0; material.mainTextureOffset = materialOffset; } } else if (handState == HandState.GoingUp) { position.y += ascentSpeed * Time.deltaTime; var grabbyPointPosition = grabbyPoint.localPosition; grabbyPointPosition.y += ascentSpeed * grabbySpeedIncrement * Time.deltaTime; var positionTraveled = Mathf.InverseLerp(MaxDescentPosition, OriginalYPosition, position.y); materialOffset.y = GetTextureOffsetModifier(positionTraveled); //Debug.LogFormat("GoingUp: Position Traveled = {0} => Mat offset = {1}", positionTraveled, materialOffset.y); material.mainTextureOffset = materialOffset; if (position.y >= OriginalYPosition) { position.y = OriginalYPosition; handState = HandState.Idle; materialOffset.y = -0.5f; material.mainTextureOffset = materialOffset; grabbyPointPosition.y = grabbyYOriginalPoint; } grabbyPoint.localPosition = grabbyPointPosition; } else if (handState == HandState.Yeeting) { var rotation = yeetingContrainerTransform.rotation; var euler = rotation.eulerAngles; yeetingRotationTimer += Time.deltaTime; var delta = Mathf.Clamp01(yeetingRotationTimer / yeetingRotationMaxTime); if (delta >= 1) { handState = HandState.Idle; yeetingRotationTimer = 0f; euler.z = yeetRotOriginal; yeetingPower = 0; AudioManager.Instance.PlayYeet(); } else { euler.z = Mathf.Lerp(yeetRotMin, yeetRotMax, delta); } if (delta > 0.8f && currentMergeableObject != null) { currentMergeableObject.transform.SetParent(null); currentMergeableObject.hoverScript.UpdateRotation = true; currentMergeableObject.YeetToPosition.YeetInit(currentMergeableObject.transform.position, opponent.backCloud, 30, 0.5f); currentMergeableObject.YeetToPosition.Yeet(Mathf.RoundToInt(currentMergeableObject.Damage * yeetingPower), onYeetEvent); currentMergeableObject = null; } rotation.eulerAngles = euler; yeetingContrainerTransform.rotation = rotation; } else if (handState == HandState.ChargingYeet) { hover1.UpdateRotation = true; hover2.UpdateRotation = true; } transform.position = position; } #endif #if UNITY_EDITOR && KEYBOARD_MODE else { var position = transform.position; float sensitivity = this.sensitivity / 1000; position.x += Input.GetAxis("Horizontal") * sensitivity; position.z += Input.GetAxis("Vertical") * sensitivity; ClampPosition(ref position); hover1.UpdateRotation = false; hover2.UpdateRotation = false; if (handState == HandState.Idle) { if (Input.GetKeyDown(KeyCode.LeftControl)) { handState = HandState.GoingDown; } else if (holdingSomethingYeetable && Input.GetKeyDown(KeyCode.Space)) { handState = HandState.ChargingYeet; } } if (handState == HandState.ChargingYeet) { if (Input.GetKeyDown(KeyCode.Space)) { yeetingPower += (oldAccel - accel).magnitude; yeetingPower = Mathf.Clamp(yeetingPower, 0, MAX_YEETING_POWER); var yeetNormalized = Mathf.Clamp01(yeetingPower / MAX_YEETING_POWER); var yoteMax = Mathf.Lerp(80, 320, yeetNormalized); var yoteMin = Mathf.Lerp(80, 160, yeetNormalized); } else if (Input.GetKeyUp(KeyCode.Space)) { //Debug.LogFormat("Yoting {0}", yeetingPower); handState = HandState.Yeeting; var yeetNormalized = Mathf.Clamp01(yeetingPower / MAX_YEETING_POWER); var yoteMax = Mathf.Lerp(80, 320, yeetNormalized); var yoteMin = Mathf.Lerp(80, 160, yeetNormalized); var direction = ourPosition.position.x <= opponentPosition.position.x; if (yeetDirection != direction) { yeetRotMin *= -1; yeetRotMax *= -1; } yeetDirection = direction; } } if (handState == HandState.GoingDown) { position.y -= descentSpeed * Time.deltaTime; var grabbyPointPosition = grabbyPoint.localPosition; grabbyPointPosition.y -= descentSpeed * grabbySpeedIncrement * Time.deltaTime; grabbyPoint.localPosition = grabbyPointPosition; var positionTraveled = Mathf.InverseLerp(MaxDescentPosition, OriginalYPosition, position.y); materialOffset.y = GetTextureOffsetModifier(positionTraveled); //Debug.LogFormat("GoingDown: Position Traveled = {0} => Mat offset = {1}", positionTraveled, materialOffset.y); material.mainTextureOffset = materialOffset; if (position.y <= MaxDescentPosition) { position.y = MaxDescentPosition; DetectObjectCollision(); handState = HandState.GoingUp; materialOffset.y = 0; material.mainTextureOffset = materialOffset; } } else if (handState == HandState.GoingUp) { position.y += ascentSpeed * Time.deltaTime; var grabbyPointPosition = grabbyPoint.localPosition; grabbyPointPosition.y += ascentSpeed * grabbySpeedIncrement * Time.deltaTime; var positionTraveled = Mathf.InverseLerp(MaxDescentPosition, OriginalYPosition, position.y); materialOffset.y = GetTextureOffsetModifier(positionTraveled); //Debug.LogFormat("GoingUp: Position Traveled = {0} => Mat offset = {1}", positionTraveled, materialOffset.y); material.mainTextureOffset = materialOffset; if (position.y >= OriginalYPosition) { position.y = OriginalYPosition; handState = HandState.Idle; materialOffset.y = -0.5f; material.mainTextureOffset = materialOffset; grabbyPointPosition.y = grabbyYOriginalPoint; } grabbyPoint.localPosition = grabbyPointPosition; } else if (handState == HandState.Yeeting) { var rotation = yeetingContrainerTransform.rotation; var euler = rotation.eulerAngles; yeetingRotationTimer += Time.deltaTime; var delta = Mathf.Clamp01(yeetingRotationTimer / yeetingRotationMaxTime); if (delta >= 1) { handState = HandState.Idle; yeetingRotationTimer = 0f; euler.z = yeetRotOriginal; yeetingPower = 0; } else { euler.z = Mathf.Lerp(yeetRotMin, yeetRotMax, delta); } if (delta > 0.8f && currentMergeableObject != null) { currentMergeableObject.transform.SetParent(null); currentMergeableObject.hoverScript.UpdateRotation = true; currentMergeableObject.YeetToPosition.YeetInit(currentMergeableObject.transform.position, opponent.backCloud, 30, 0.5f); currentMergeableObject.YeetToPosition.Yeet(Mathf.RoundToInt(currentMergeableObject.Damage * yeetingPower), onYeetEvent); currentMergeableObject = null; } rotation.eulerAngles = euler; yeetingContrainerTransform.rotation = rotation; } else if (handState == HandState.ChargingYeet) { hover1.UpdateRotation = true; hover2.UpdateRotation = true; } transform.position = position; } #endif }