// Update is called once per frame void Update() { if (input.axis.magnitude > 0.1f) { Vector3 direction = Player.instance.hmdTransform.TransformDirection(new Vector3(input.axis.x, 0, input.axis.y)); //transform.position += speed * Time.deltaTime * Vector3.ProjectOnPlane(direction, Vector3.up); characterController.Move(speed * Time.deltaTime * Vector3.ProjectOnPlane(direction, Vector3.up) - new Vector3(0, 9.81f, 0) * Time.deltaTime); } }
// Update is called once per frame void Update() { // Quit code if (Input.GetKey(KeyCode.Escape)) { Application.Quit(); } // Detect ground collision ground = GetGroundHit(); // Add gravity acceleration and clamp fall speed to the terminal velocity velocityVertical += Time.deltaTime * gravity; velocityVertical = Mathf.Clamp(velocityVertical, terminalVelocity, float.MaxValue); // Sprint and crouch logic isSprinting = Input.GetKey(KeyCode.LeftShift); isCrouching = Input.GetKey(KeyCode.LeftControl); // Jump code if (isGrounded && !groundTooSloped && Input.GetAxisRaw("Jump") != 0f) { velocityVertical = jumpVelocity; isGrounded = false; } // Get horizontal velocity velocityHorizontal = CalculateVelocityHorizontal(); // Combine vertical and horizontal velocity if (!isGrounded) { velocitySum = velocityHorizontal + Vector3.up * velocityVertical; } else { if (groundTooSloped) { velocitySum = Vector3.ProjectOnPlane(velocityHorizontal + Vector3.up * velocityVertical, ground.normal); } else { velocityVertical -= gravity * Time.deltaTime; velocitySum = Vector3.ProjectOnPlane(velocityHorizontal, ground.normal).normalized *velocityHorizontal.magnitude + Vector3.up * velocityVertical; } } // Move and rotate the player controller.Move(velocitySum * Time.deltaTime); transform.Rotate(CameraController.sensitivity * Input.GetAxisRaw("Mouse X") * Vector3.up, Space.Self); // After movement reset vertical velocity if player is grounded if (isGrounded && !groundTooSloped) { velocityVertical = 0; } }
protected override Vector3 GetShootDirection() { var camera = Camera.main; var shootingOrigin = Refs.shootingOrigin.position; var direction = shootingOrigin - camera.transform.position; direction = Vector3.Project(direction, camera.transform.forward); var mousePosition = Input.mousePosition; mousePosition.z = direction.magnitude; var mousePos = camera.ScreenToWorldPoint(mousePosition); direction = mousePos - shootingOrigin; direction = Vector3.ProjectOnPlane(direction, Vector3.up).normalized; return(direction); }
void FixedUpdate() { OnNavigation(); // always move along the camera forward as it is the direction that it being aimed at Vector3 desiredMove = transform.forward * m_Input.y + transform.right * m_Input.x; // get a normal for the surface that is being touched to move along it RaycastHit hitInfo; Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo, m_CharacterController.height / 2f, Physics.AllLayers, QueryTriggerInteraction.Ignore); desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized; m_MoveDir.x = desiredMove.x * m_Speed; m_MoveDir.z = desiredMove.z * m_Speed; if (!GamingNavigation) { var offset = m_Input.y <= (-1f + Mathf.Epsilon) ? Quaternion.Euler(0, 180f, 0) : Quaternion.identity; if (m_Input != Vector2.zero) { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(desiredMove) * offset, m_RotationSpeed); } } RotateView(); if (m_Rigidbody.useGravity) { if (m_CharacterController.isGrounded) { m_MoveDir.y = -m_StickToGroundForce; if (m_Jump) { m_MoveDir.y = m_JumpSpeed; PlayJumpSound(); m_Jump = false; m_Jumping = true; } } else { m_MoveDir += Physics.gravity * m_GravityMultiplier * Time.fixedDeltaTime; } } else { m_MoveDir.y = 0; } if (m_CharacterController.enabled) { m_CollisionFlags = m_CharacterController.Move(m_MoveDir * Time.fixedDeltaTime); } ProgressStepCycle(m_Speed); UpdateCameraPosition(m_Speed); m_MouseLook.UpdateCursorLock(); if (transform.position.y < m_MaxFallDistance) { m_WalkInstructionSelector.GetValue().Reset(m_ResetFallOffset); } }
protected override void OnUpdate() { if (startCountdownServerSystem.raceHasStarted) { var tick = ghostPredictionSystemGroup.PredictingTick; Entities.ForEach((Entity carEntity, DynamicBuffer <PlayerInput> inputBuffer, ref SynchronizedCarComponent carComponent, ref HealthComponent healthComponent, ref CarComponent serverCarComponent, ref PhysicsVelocity velocity, ref Rotation rotation) => { PlayerInput input; inputBuffer.GetDataAtTick(tick, out input); bool playerCanControl = healthComponent.Health > 0 && EntityManager.GetComponentData <ProgressionComponent>(carEntity).CrossedCheckpoints < checkpointInitializationSystem.numberOfCheckpoints * GameSession.serverSession.laps; bool keyForwardPressed = Convert.ToBoolean(input.keyForwardPressed) && playerCanControl; bool keyBackwardPressed = Convert.ToBoolean(input.keyBackwardPressed) && playerCanControl; bool keyTurnRightPressed = Convert.ToBoolean(input.keyTurnRightPressed) && playerCanControl; bool keyTurnLeftPressed = Convert.ToBoolean(input.keyTurnLeftPressed) && playerCanControl; if (keyTurnRightPressed && !keyTurnLeftPressed) { if (carComponent.SteerAngle < 0) { carComponent.SteerAngle = 0; } if (carComponent.SteerAngle < SerializedFields.singleton.maxSteerAngle) { carComponent.SteerAngle += SerializedFields.singleton.wheelSteeringAngularSpeed * simulationDeltaTime; if (carComponent.SteerAngle > SerializedFields.singleton.maxSteerAngle) { carComponent.SteerAngle = SerializedFields.singleton.maxSteerAngle; } } } else if (keyTurnLeftPressed && !keyTurnRightPressed) { if (carComponent.SteerAngle > 0) { carComponent.SteerAngle = 0; } if (carComponent.SteerAngle > -SerializedFields.singleton.maxSteerAngle) { carComponent.SteerAngle -= SerializedFields.singleton.wheelSteeringAngularSpeed * simulationDeltaTime; if (carComponent.SteerAngle < -SerializedFields.singleton.maxSteerAngle) { carComponent.SteerAngle = -SerializedFields.singleton.maxSteerAngle; } } } else if (!keyTurnRightPressed && !keyTurnLeftPressed) { if (carComponent.SteerAngle > 0) { carComponent.SteerAngle -= SerializedFields.singleton.idleWheelSteeringAngularSpeed * simulationDeltaTime; if (carComponent.SteerAngle < 0) { carComponent.SteerAngle = 0; } } else if (carComponent.SteerAngle < 0) { carComponent.SteerAngle += SerializedFields.singleton.idleWheelSteeringAngularSpeed * simulationDeltaTime; if (carComponent.SteerAngle > 0) { carComponent.SteerAngle = 0; } } } float turningRadiusInverse = CalculateTurningRadiusInverse(carComponent.SteerAngle); Vector3 localVelocity = math.mul(math.inverse(rotation.Value), velocity.Linear); if (localVelocity.magnitude > 0) { Vector3 resistance = -localVelocity.normalized * localVelocity.sqrMagnitude * SerializedFields.singleton.forwardAcceleration / math.pow(SerializedFields.singleton.maxCarSpeed, 2) * simulationDeltaTime; localVelocity += resistance; } if (localVelocity.x > 0) { localVelocity -= new Vector3(SerializedFields.singleton.friction * 9.81f * simulationDeltaTime, 0, 0); if (localVelocity.x < 0) { localVelocity = new Vector3(0, localVelocity.y, localVelocity.z); } } else if (localVelocity.x < 0) { localVelocity += new Vector3(SerializedFields.singleton.friction * 9.81f * simulationDeltaTime, 0, 0); if (localVelocity.x > 0) { localVelocity = new Vector3(0, localVelocity.y, localVelocity.z); } } if (!serverCarComponent.GoingBackward) { if (keyForwardPressed && !keyBackwardPressed) { if (localVelocity.z < 0) { localVelocity += new Vector3(0, 0, SerializedFields.singleton.friction * 9.81f * simulationDeltaTime); } else { localVelocity += new Vector3(0, 0, SerializedFields.singleton.forwardAcceleration * simulationDeltaTime); } } else if (keyBackwardPressed && !keyForwardPressed) { if (localVelocity.z > 0) { localVelocity -= new Vector3(0, 0, SerializedFields.singleton.brakeAcceleration * simulationDeltaTime); if (localVelocity.z < 0) { localVelocity = new Vector3(localVelocity.x, localVelocity.y, 0); } } else if (localVelocity.z < 0) { localVelocity += new Vector3(0, 0, SerializedFields.singleton.brakeAcceleration * simulationDeltaTime); if (localVelocity.z > 0) { localVelocity = new Vector3(localVelocity.x, localVelocity.y, 0); } } if (Vector3.ProjectOnPlane(localVelocity, Vector3.up).magnitude < 0.1f) { serverCarComponent.GoingBackward = true; } } else { if (localVelocity.z > 0) { localVelocity -= new Vector3(0, 0, SerializedFields.singleton.idleDeacceleration * simulationDeltaTime); if (localVelocity.z < 0) { localVelocity = new Vector3(localVelocity.x, localVelocity.y, 0); } } else if (localVelocity.z < 0) { localVelocity += new Vector3(0, 0, SerializedFields.singleton.friction * 9.81f * simulationDeltaTime); if (localVelocity.z > 0) { localVelocity = new Vector3(localVelocity.x, localVelocity.y, 0); } } } } else { if (keyForwardPressed && !keyBackwardPressed) { if (localVelocity.z > 0) { localVelocity -= new Vector3(0, 0, SerializedFields.singleton.brakeAcceleration * simulationDeltaTime); if (localVelocity.z < 0) { localVelocity = new Vector3(localVelocity.x, localVelocity.y, 0); } } else if (localVelocity.z < 0) { localVelocity += new Vector3(0, 0, SerializedFields.singleton.brakeAcceleration * simulationDeltaTime); if (localVelocity.z > 0) { localVelocity = new Vector3(localVelocity.x, localVelocity.y, 0); } } if (Vector3.ProjectOnPlane(localVelocity, Vector3.up).magnitude < 0.1f) { serverCarComponent.GoingBackward = false; } } else if (keyBackwardPressed && !keyForwardPressed) { if (localVelocity.z > -SerializedFields.singleton.backwardTopSpeed) { localVelocity -= new Vector3(0, 0, SerializedFields.singleton.backwardAcceleration * simulationDeltaTime); if (localVelocity.z < -SerializedFields.singleton.backwardTopSpeed) { localVelocity = new Vector3(localVelocity.x, localVelocity.y, -SerializedFields.singleton.backwardTopSpeed); } } else if (localVelocity.z < -SerializedFields.singleton.backwardTopSpeed) { localVelocity += new Vector3(0, 0, SerializedFields.singleton.brakeAcceleration * simulationDeltaTime); if (localVelocity.z > -SerializedFields.singleton.backwardTopSpeed) { localVelocity = new Vector3(localVelocity.x, localVelocity.y, -SerializedFields.singleton.backwardTopSpeed); } } } else { if (localVelocity.z > 0) { localVelocity -= new Vector3(0, 0, SerializedFields.singleton.friction * 9.81f * simulationDeltaTime); if (localVelocity.z < 0) { localVelocity = new Vector3(localVelocity.x, localVelocity.y, 0); } } else if (localVelocity.z < 0) { if (localVelocity.z < -SerializedFields.singleton.backwardTopSpeed) { localVelocity += new Vector3(0, 0, SerializedFields.singleton.friction * 9.81f * simulationDeltaTime); } else { localVelocity += new Vector3(0, 0, SerializedFields.singleton.idleDeacceleration * simulationDeltaTime); if (localVelocity.z > 0) { localVelocity = new Vector3(localVelocity.x, localVelocity.y, 0); } } } } } Vector3 localAngularVelocity = velocity.Angular; // no need to inverse transform if (localAngularVelocity.y > SerializedFields.singleton.maxAngularVelocity) { localAngularVelocity.y = SerializedFields.singleton.maxAngularVelocity; } else if (localAngularVelocity.y < -SerializedFields.singleton.maxAngularVelocity) { localAngularVelocity.y = -SerializedFields.singleton.maxAngularVelocity; } Vector3 transformUp = math.mul(rotation.Value, new float3(0, 1, 0)); float targetAngularVelocityY = Mathf.Min(Vector3.ProjectOnPlane(velocity.Linear, transformUp).magnitude / SerializedFields.singleton.minSpeedForFullRotation, 1) * carComponent.SteerAngle / SerializedFields.singleton.maxSteerAngle * SerializedFields.singleton.maxTurningAngularVelocity * (serverCarComponent.GoingBackward ? -1 : 1); if (targetAngularVelocityY > localAngularVelocity.y) { localAngularVelocity += new Vector3(0, SerializedFields.singleton.angularAcceleration * simulationDeltaTime, 0); if (localAngularVelocity.y > targetAngularVelocityY) { localAngularVelocity = new Vector3(localAngularVelocity.x, targetAngularVelocityY, localAngularVelocity.z); } } else if (targetAngularVelocityY < localAngularVelocity.y) { localAngularVelocity -= new Vector3(0, SerializedFields.singleton.angularAcceleration * simulationDeltaTime, 0); if (localAngularVelocity.y < targetAngularVelocityY) { localAngularVelocity = new Vector3(localAngularVelocity.x, targetAngularVelocityY, localAngularVelocity.z); } } localVelocity = Vector3.ProjectOnPlane(localVelocity, Vector3.forward) + Quaternion.AngleAxis(localAngularVelocity.y * simulationDeltaTime, Vector3.up) * Vector3.Project(localVelocity, Vector3.forward); if (EntityManager.GetComponentData <BoostComponent>(carEntity).RemainingTime > 0) { localVelocity += new Vector3(0, 0, SerializedFields.singleton.boostPowerupAcceleration * simulationDeltaTime); } velocity.Angular = localAngularVelocity; // no need to transform velocity.Linear = math.mul(rotation.Value, localVelocity); }); } }