// LOAD PLUGINS /// <summary>Loads Default Components</summary> private void OnValidate() { if (!movement) { movement = Resources.Load <MovementModifiers>("Player/Movement/_default/DefaultMovementModifiers"); //LOAD MOVEMENT MODS } //if (!keys) keys = Resources.Load<MovementKeys>("Player/Movement/_default/DefaultMovementKeys"); //LOAD DEFAULT MOVE KEYS if (!motor) { motor = Resources.Load <CharacterMotor>("Player/Movement/_default/DefaultCharacterMotor"); //LOAD DEFAULT MOTOR } }
/// <summary> /// This recalculates the agent velocity based on the current input axis' /// </summary> protected virtual Vector3 CalculateVelocity(MovementInput movement, MovementModifiers movementConfig, NavMeshAgent agent) { //FACE DIRECTION OF TRAVEL //if (config.faceCameraDirection && Camera.main) transform.LookAt(agent.velocity, Vector3.up); if (movement.verticalInput != 0 || movement.horizontalInput != 0 || movement.strafeLeft || movement.strafeRight) { /* //DEPRECIATED * Vector3 input = new Vector3(movement.horizontalInput, 0, movement.verticalInput); * if (input.magnitude > 1) input = input.normalized; * * //Vector3 angles = transform.rotation.eulerAngles; * Vector3 angles = * ( * (movementConfig.faceCameraDirection && Camera.main) * ? new Vector3(agent.transform.rotation.eulerAngles.x, Camera.main.transform.rotation.eulerAngles.y, agent.transform.rotation.eulerAngles.z) * : agent.transform.rotation.eulerAngles * ); * angles.x = 0; * Quaternion rotation = Quaternion.Euler(angles); * * Vector3 direction = rotation * input; */ Vector3 verticalDirection = Vector3.zero; Vector3 horizontalDirection = Vector3.zero; Vector3 newVelocity = Vector3.zero; //SPEED FACTOR float factor = movement.running ? movementConfig.runSpeedScale : movementConfig.walkSpeedScale; //running factor *= movement.sneaking ? movementConfig.sneakSpeedScale : 1f; //sneaking if (movement.verticalInput != 0) { if (movement.verticalInput > 0 && Camera.main) { verticalDirection = Camera.main.transform.forward; } else if (movement.verticalInput < 0 && Camera.main) { verticalDirection = -Camera.main.transform.forward; } //newVelocity = verticalDirection * Mathf.Abs(movement.verticalInput);// * agent.speed * factor * movementConfig.moveSpeedMultiplier; //if (Camera.main) direction = rotation * Camera.main.transform.forward; //if (Camera.main) direction = rotation * -Camera.main.transform.forward; //newVelocity = direction * Mathf.Abs(movement.verticalInput) * agent.speed * factor * movementConfig.moveSpeedMultiplier; //* movementConfig.backpedalSpeedScale } if (movement.horizontalInput != 0) //STRAFE { if (movement.horizontalInput > 0 && Camera.main) { horizontalDirection = Camera.main.transform.right; } if (movement.horizontalInput < 0 && Camera.main) { horizontalDirection = -Camera.main.transform.right; } //newVelocity = horizontalDirection * Mathf.Abs(movement.horizontalInput);// * agent.speed * factor * movementConfig.moveSpeedMultiplier; //if (movement.horizontalInput > 0 && Camera.main) direction = rotation * -Camera.main.transform.right; //if (movement.horizontalInput < 0 && Camera.main) direction = rotation * Camera.main.transform.right; //NOTE: We do not want to factor run speed into strafing...we do not want both multipliers to make diagonal speed faster than forward speed. //float factor = running ? config.runSpeedScale : config.walkSpeedScale; } newVelocity = (horizontalDirection * Mathf.Abs(movement.horizontalInput)) + (verticalDirection * Mathf.Abs(movement.verticalInput)); newVelocity *= agent.speed * factor * movementConfig.moveSpeedMultiplier; //FACTOR IN SPEED //STRAFE LEFT if (movement.strafeLeft) { if (!movement.strafeRight) //NOTE: Holding both turn buttons cancels out turning { //direction = -agent.transform.right; //if (agent.velocity == Vector3.zero) agent.velocity = transform.forward.normalized; //FORWARD VELOCITY if (movementConfig.turnWhileStrafing) { agent.transform.Rotate(0, -1.0f * movementConfig.turnSpeedMultiplier * Time.deltaTime * 100f, 0); //TURN WHILE STRAFING } newVelocity = -agent.transform.right * 5f * agent.speed * movementConfig.strafeSpeedScale * movementConfig.moveSpeedMultiplier; } } //STRAFE RIGHT else if (movement.strafeRight) { //direction = agent.transform.right; //if (agent.velocity == Vector3.zero) agent.velocity = transform.forward.normalized; //FORWARD VELOCITY if (movementConfig.turnWhileStrafing) { agent.transform.Rotate(0, 1.0f * movementConfig.turnSpeedMultiplier * Time.deltaTime * 100f, 0); //TURN WHILE STRAFING } //newVelocity = direction * horizontalMovementInput * agent.speed * config.strafeSpeedScale * config.moveSpeedMultiplier; newVelocity = agent.transform.right * 5f * agent.speed * movementConfig.strafeSpeedScale * movementConfig.moveSpeedMultiplier; } if (!movement.strafeLeft && !movement.strafeRight) { //agent.transform.Rotate(0, movement.horizontalInput * movementConfig.turnSpeedMultiplier * Time.deltaTime * 100f, 0); //SET ROTATION - ON TRANSFORM - NOT WHILE STRAFING } if (lookInMoveDirection && (horizontalDirection + verticalDirection) != Vector3.zero) { agent.transform.rotation = Quaternion.Slerp(agent.transform.rotation, Quaternion.LookRotation(horizontalDirection + verticalDirection), agent.angularSpeed * factor); } //if () agent.gameObject.transform.LookAt(agent.gameObject.transform.position + direction, Vector3.up); //LOOK AT MOVE DIRECTION return(newVelocity); //SET VELOCITY - ON NAVMESH AGENT } else { // -- required? if (agent.isOnNavMesh) { agent.ResetPath(); } return(Vector3.zero); } }
internal override Vector3 GetVelocity(MovementInput movement, MovementModifiers movementConfig, NavMeshAgent agent) { return(CalculateVelocity(movement, movementConfig, agent)); }
/// <summary> /// This recalculates the agent velocity based on the current input axis' /// </summary> protected virtual Vector3 CalculateVelocity(MovementInput movement, MovementModifiers movementConfig, NavMeshAgent agent) { //FACE DIRECTION OF TRAVEL //if (config.faceCameraDirection && Camera.main) transform.LookAt(agent.velocity, Vector3.up); if (movement.verticalInput != 0 || movement.horizontalInput != 0 || movement.strafeLeft || movement.strafeRight) { Vector3 input = new Vector3(movement.horizontalInput, 0, movement.verticalInput); if (input.magnitude > 1) { input = input.normalized; } //Vector3 angles = transform.rotation.eulerAngles; Vector3 angles = ( (movementConfig.faceCameraDirection && Camera.main) ? new Vector3(agent.transform.rotation.eulerAngles.x, Camera.main.transform.rotation.eulerAngles.y, agent.transform.rotation.eulerAngles.z) : agent.transform.rotation.eulerAngles ); angles.x = 0; Quaternion rotation = Quaternion.Euler(angles); Vector3 direction = rotation * input; Vector3 newVelocity = Vector3.zero; if (movement.verticalInput > 0) // -- Movement: Forward { float factor = movement.running ? movementConfig.runSpeedScale : movementConfig.walkSpeedScale; //running factor *= movement.sneaking ? movementConfig.sneakSpeedScale : 1f; //sneaking newVelocity = direction * movement.verticalInput * agent.speed * factor * movementConfig.moveSpeedMultiplier; } else if (movement.verticalInput < 0) // -- Movement: Backward { float factor = movement.running ? movementConfig.runSpeedScale : movementConfig.walkSpeedScale; //running factor *= movement.sneaking ? movementConfig.sneakSpeedScale : 1f; //sneaking newVelocity = direction * Mathf.Abs(movement.verticalInput) * agent.speed * factor * movementConfig.backpedalSpeedScale * movementConfig.moveSpeedMultiplier; } else if (movement.horizontalInput != 0) //STRAFE { //NOTE: We do not want to factor run speed into strafing...we do not want both multipliers to make diagonal speed faster than forward speed. //float factor = running ? config.runSpeedScale : config.walkSpeedScale; newVelocity = direction * agent.speed * movementConfig.strafeSpeedScale * movementConfig.moveSpeedMultiplier; } //STRAFE LEFT if (movement.strafeLeft) { if (!movement.strafeRight) //NOTE: Holding both turn buttons cancels out turning { //direction = -agent.transform.right; //if (agent.velocity == Vector3.zero) agent.velocity = transform.forward.normalized; //FORWARD VELOCITY if (movementConfig.turnWhileStrafing) { agent.transform.Rotate(0, -1.0f * movementConfig.turnSpeedMultiplier * Time.deltaTime * 100f, 0); //TURN WHILE STRAFING } newVelocity = -agent.transform.right * 5f * agent.speed * movementConfig.strafeSpeedScale * movementConfig.moveSpeedMultiplier; } } //STRAFE RIGHT else if (movement.strafeRight) { //direction = agent.transform.right; //if (agent.velocity == Vector3.zero) agent.velocity = transform.forward.normalized; //FORWARD VELOCITY if (movementConfig.turnWhileStrafing) { agent.transform.Rotate(0, 1.0f * movementConfig.turnSpeedMultiplier * Time.deltaTime * 100f, 0); //TURN WHILE STRAFING } //newVelocity = direction * horizontalMovementInput * agent.speed * config.strafeSpeedScale * config.moveSpeedMultiplier; newVelocity = agent.transform.right * 5f * agent.speed * movementConfig.strafeSpeedScale * movementConfig.moveSpeedMultiplier; } if (!movement.strafeLeft && !movement.strafeRight) { agent.transform.Rotate(0, movement.horizontalInput * movementConfig.turnSpeedMultiplier * Time.deltaTime * 100f, 0); //SET ROTATION - ON TRANSFORM - NOT WHILE STRAFING } return(newVelocity); //SET VELOCITY - ON NAVMESH AGENT } else { // -- required? if (agent.isOnNavMesh) { agent.ResetPath(); } return(Vector3.zero); } }
//CALCULATE VELOCITY /// <summary> /// Calculates an agent's velocity based on a set of modifiers /// </summary> protected virtual Vector3 CalculateVelocity(MovementInput input, MovementModifiers modifiers, NavMeshAgent agent) { Vector3 movement = new Vector3( input.verticalInput, 0.0f, -input.horizontalInput); //movement.x = Camera.main.transform.forward; //SPEED FACTOR //NOTE: Run speed and Sneak speed will blend together. float factor = input.running ? modifiers.runSpeedScale : modifiers.walkSpeedScale; //running factor *= input.sneaking ? modifiers.sneakSpeedScale : 1f; //sneaking if (movement != Vector3.zero) { agent.transform.rotation = Quaternion.Slerp(agent.transform.rotation, Quaternion.LookRotation(movement), agent.angularSpeed * factor); } agent.transform.Translate(movement * agent.speed * factor * modifiers.moveSpeedMultiplier * Time.deltaTime, Space.World);// * agent.speed * factor * modifiers.moveSpeedMultiplier return(agent.velocity); /* * if (input.verticalInput != 0 || input.horizontalInput != 0 || input.strafeLeft || input.strafeRight) * { * //GET ROTATION * Vector3 angles = agent.transform.rotation.eulerAngles; * //( * //(modifiers.faceCameraDirection && Camera.main) * //? new Vector3(agent.transform.rotation.eulerAngles.x, Camera.main.transform.rotation.eulerAngles.y, agent.transform.rotation.eulerAngles.z) * //: agent.transform.rotation.eulerAngles * //); * angles.x = 0; * Quaternion rotation = Quaternion.Euler(angles); * * //GET INPUT VECTOR * Vector3 inputVector = new Vector3(input.horizontalInput, 0, input.verticalInput); * * if (inputVector.magnitude > 1) inputVector = inputVector.normalized; //NORMALIZE INPUT * * //CALCULATE DIRECTION * Vector3 direction = rotation * inputVector; * * //INITIALIZE NEW VELOCITY * Vector3 newVelocity = Vector3.zero; * * //SPEED FACTOR * //NOTE: Run speed and Sneak speed will blend together. * float factor = input.running ? modifiers.runSpeedScale : modifiers.walkSpeedScale; //running * factor *= input.sneaking ? modifiers.sneakSpeedScale : 1f; //sneaking * * //CALCULATE FORWARD VELOCITY * if (input.verticalInput > 0) * { * direction = Vector3.forward;// agent.transform.forward; * agent.transform.LookAt(Vector3.forward, Vector3.up); * //agent.transform.forward = direction; //FACE DIRECTION OF TRAVEL * newVelocity = direction * input.verticalInput * agent.speed * factor * modifiers.moveSpeedMultiplier; //velocity * } * //CALCULATE BACKWARDS VELOCITY * else if (input.verticalInput < 0) * { * direction = -Vector3.forward;// -agent.transform.forward; * agent.transform.LookAt(-Vector3.forward, Vector3.up); * //agent.transform.forward = direction; //FACE DIRECTION OF TRAVEL * newVelocity = direction * Mathf.Abs(input.verticalInput) * agent.speed * factor * modifiers.backpedalSpeedScale * modifiers.moveSpeedMultiplier; * } * * //CALCULATE RIGHT VELOCITY * if (input.horizontalInput > 0) * { * direction = Vector3.right;// agent.transform.right; * agent.transform.LookAt(Vector3.right, Vector3.up); * //agent.transform.forward = direction; //FACE DIRECTION OF TRAVEL * newVelocity = direction * input.horizontalInput * agent.speed * factor * modifiers.moveSpeedMultiplier; //velocity * } * //CALCULATE LEFT VELOCITY * else if (input.horizontalInput < 0) * { * direction = -Vector3.right;// -agent.transform.right; * agent.transform.LookAt(-Vector3.right, Vector3.up); * //agent.transform.forward = direction; //FACE DIRECTION OF TRAVEL * newVelocity = direction * Mathf.Abs(input.horizontalInput) * agent.speed * factor * modifiers.moveSpeedMultiplier; * } * //CALCULATE STRAFE VELOCITY * //else if (input.horizontalInput != 0) * //{ * //NOTE: We do not want to factor run speed into strafing...we do not want both multipliers to make diagonal speed faster than forward speed. * //float factor = running ? config.runSpeedScale : config.walkSpeedScale; //running * //factor *= input.sneaking ? modifiers.sneakSpeedScale : 1f; //sneaking * // newVelocity = direction * agent.speed * modifiers.strafeSpeedScale * modifiers.moveSpeedMultiplier; * //} * * //CALCULATE STRAFE LEFT * if (input.strafeLeft) * { * if (!input.strafeRight) //NOTE: Holding both turn buttons cancels out turning * { * //direction = -agent.transform.right; * //if (agent.velocity == Vector3.zero) agent.velocity = transform.forward.normalized; //FORWARD VELOCITY * if (modifiers.turnWhileStrafing) agent.transform.Rotate(0, -1.0f * modifiers.turnSpeedMultiplier * Time.deltaTime * 100f, 0); //TURN WHILE STRAFING * newVelocity = -agent.transform.right * 5f * agent.speed * modifiers.strafeSpeedScale * modifiers.moveSpeedMultiplier; * } * } * //CALCULATE STRAFE RIGHT * else if (input.strafeRight) * { * //direction = agent.transform.right; * //if (agent.velocity == Vector3.zero) agent.velocity = transform.forward.normalized; //FORWARD VELOCITY * if (modifiers.turnWhileStrafing) agent.transform.Rotate(0, 1.0f * modifiers.turnSpeedMultiplier * Time.deltaTime * 100f, 0); //TURN WHILE STRAFING * //newVelocity = direction * horizontalMovementInput * agent.speed * config.strafeSpeedScale * config.moveSpeedMultiplier; * newVelocity = agent.transform.right * 5f * agent.speed * modifiers.strafeSpeedScale * modifiers.moveSpeedMultiplier; * } * //TURN * //else //if (!movement.movementStrafeLeft && !movement.movementStrafeRight) * //{ * // agent.transform.Rotate(0, input.horizontalInput * modifiers.turnSpeedMultiplier * Time.deltaTime * 100f, 0); //SET ROTATION - ON TRANSFORM - NOT WHILE STRAFING * //} * * * //agent.transform.forward = direction; //FACE DIRECTION OF TRAVEL * //NOTE: Choose one LookAt or the other or the last one cancels the rest out. * //agent.transform.LookAt(direction, agent.transform.up); //look in the direction you are going * //agent.transform.LookAt(direction, Vector3.up); //rotate around worldspace up * * //RETURN NEW VELOCITY * return newVelocity; * } * //else * //{ * // if (agent.isOnNavMesh) agent.ResetPath(); //NOTE: May not be required, but called as a failsafe. * return Vector3.zero; * //} */ }
internal abstract Vector3 GetVelocity(MovementInput movement, MovementModifiers movementConfig, NavMeshAgent agent);