Skip to content

Instantly share code, notes, and snippets.

@whoisryosuke
Created August 23, 2021 23:33
Show Gist options
  • Select an option

  • Save whoisryosuke/08f578c0fb3d5606143dc3548cbf0d9a to your computer and use it in GitHub Desktop.

Select an option

Save whoisryosuke/08f578c0fb3d5606143dc3548cbf0d9a to your computer and use it in GitHub Desktop.
Unity - Default player movement provided by Unity in some templates.
  1. Drop the PlayerControllerFPS prefab into the scene
  2. Remove any primary cameras from the scene (the prefab comes with one).
  3. Position as needed. Make sure Ground Check component lines up with Z-axis floor.
  4. Press play!
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &738646681595172696
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 738646681595172700}
- component: {fileID: 738646681595172699}
- component: {fileID: 738646681595172697}
- component: {fileID: 738646681595172698}
- component: {fileID: 738646681595172702}
m_Layer: 9
m_Name: Main Camera
m_TagString: MainCamera
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &738646681595172700
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 738646681595172696}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0.7, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 738646681744756156}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!20 &738646681595172699
Camera:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 738646681595172696}
m_Enabled: 1
serializedVersion: 2
m_ClearFlags: 1
m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0}
m_projectionMatrixMode: 1
m_GateFitMode: 2
m_FOVAxisMode: 1
m_SensorSize: {x: 36, y: 24}
m_LensShift: {x: 0, y: 0}
m_FocalLength: 21.451567
m_NormalizedViewPortRect:
serializedVersion: 2
x: 0
y: 0
width: 1
height: 1
near clip plane: 0.1
far clip plane: 1000
field of view: 50.53402
orthographic: 0
orthographic size: 5
m_Depth: -1
m_CullingMask:
serializedVersion: 2
m_Bits: 4294967295
m_RenderingPath: -1
m_TargetTexture: {fileID: 0}
m_TargetDisplay: 0
m_TargetEye: 3
m_HDR: 0
m_AllowMSAA: 0
m_AllowDynamicResolution: 0
m_ForceIntoRT: 1
m_OcclusionCulling: 1
m_StereoConvergence: 10
m_StereoSeparation: 0.022
--- !u!81 &738646681595172697
AudioListener:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 738646681595172696}
m_Enabled: 1
--- !u!114 &738646681595172698
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 738646681595172696}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 23c1ce4fb46143f46bc5cb5224c934f6, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Version: 7
m_ObsoleteRenderingPath: 0
m_ObsoleteFrameSettings:
overrides: 0
enableShadow: 0
enableContactShadows: 0
enableShadowMask: 0
enableSSR: 0
enableSSAO: 0
enableSubsurfaceScattering: 0
enableTransmission: 0
enableAtmosphericScattering: 0
enableVolumetrics: 0
enableReprojectionForVolumetrics: 0
enableLightLayers: 0
enableExposureControl: 1
diffuseGlobalDimmer: 0
specularGlobalDimmer: 0
shaderLitMode: 0
enableDepthPrepassWithDeferredRendering: 0
enableTransparentPrepass: 0
enableMotionVectors: 0
enableObjectMotionVectors: 0
enableDecals: 0
enableRoughRefraction: 0
enableTransparentPostpass: 0
enableDistortion: 0
enablePostprocess: 0
enableOpaqueObjects: 0
enableTransparentObjects: 0
enableRealtimePlanarReflection: 0
enableMSAA: 0
enableAsyncCompute: 0
runLightListAsync: 0
runSSRAsync: 0
runSSAOAsync: 0
runContactShadowsAsync: 0
runVolumeVoxelizationAsync: 0
lightLoopSettings:
overrides: 0
enableDeferredTileAndCluster: 0
enableComputeLightEvaluation: 0
enableComputeLightVariants: 0
enableComputeMaterialVariants: 0
enableFptlForForwardOpaque: 0
enableBigTilePrepass: 0
isFptlEnabled: 0
clearColorMode: 0
backgroundColorHDR: {r: 0.025, g: 0.07, b: 0.19, a: 0}
clearDepth: 1
volumeLayerMask:
serializedVersion: 2
m_Bits: 1
volumeAnchorOverride: {fileID: 0}
antialiasing: 2
SMAAQuality: 2
dithering: 1
stopNaNs: 0
taaSharpenStrength: 0.1
TAAQuality: 1
taaHistorySharpening: 0.1
taaAntiFlicker: 0.7
taaMotionVectorRejection: 0
taaAntiHistoryRinging: 0
physicalParameters:
m_Iso: 200
m_ShutterSpeed: 0.005
m_Aperture: 2
m_BladeCount: 9
m_Curvature: {x: 2, y: 11}
m_BarrelClipping: 0.25
m_Anamorphism: 0
flipYMode: 0
xrRendering: 1
fullscreenPassthrough: 0
allowDynamicResolution: 0
customRenderingSettings: 0
invertFaceCulling: 0
probeLayerMask:
serializedVersion: 2
m_Bits: 4294967295
hasPersistentHistory: 0
exposureTarget: {fileID: 0}
m_RenderingPathCustomFrameSettings:
bitDatas:
data1: 68906302766941
data2: 4539628424389459968
lodBias: 1
lodBiasMode: 0
lodBiasQualityLevel: 0
maximumLODLevel: 0
maximumLODLevelMode: 0
maximumLODLevelQualityLevel: 0
sssQualityMode: 0
sssQualityLevel: 0
sssCustomSampleBudget: 20
materialQuality: 0
renderingPathCustomFrameSettingsOverrideMask:
mask:
data1: 0
data2: 0
defaultFrameSettings: 0
--- !u!114 &738646681595172702
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 738646681595172696}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4eb578550bc4f824e97f0a72eac1f3a5, type: 3}
m_Name:
m_EditorClassIdentifier:
mouseSensitivity: 100
playerBody: {fileID: 738646681744756156}
--- !u!1 &738646681744756154
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 738646681744756156}
- component: {fileID: 738646681744756155}
- component: {fileID: 738646681744756157}
m_Layer: 9
m_Name: PlayerControllerFPS
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &738646681744756156
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 738646681744756154}
m_LocalRotation: {x: 0, y: 0.9016779, z: 0, w: 0.43240842}
m_LocalPosition: {x: -5.858, y: 4.442, z: 8.271}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 738646681595172700}
- {fileID: 738646681763345176}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 128.759, z: 0}
--- !u!143 &738646681744756155
CharacterController:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 738646681744756154}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 2
m_Height: 2
m_Radius: 0.3
m_SlopeLimit: 45
m_StepOffset: 0.7
m_SkinWidth: 0.08
m_MinMoveDistance: 0.001
m_Center: {x: 0, y: 0, z: 0}
--- !u!114 &738646681744756157
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 738646681744756154}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 87f6dfceb3e39a947a312f7eeaa2a113, type: 3}
m_Name:
m_EditorClassIdentifier:
controller: {fileID: 738646681744756155}
speed: 5
gravity: -10
jumpHeight: 2
groundCheck: {fileID: 738646681763345176}
groundDistance: 0.4
groundMask:
serializedVersion: 2
m_Bits: 0
--- !u!1 &738646681763345175
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 738646681763345176}
m_Layer: 9
m_Name: GroundCheck
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &738646681763345176
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 738646681763345175}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: -1.25, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 738646681744756156}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
fileFormatVersion: 2
guid: 573ac53c334415945bf239de2c2f0511
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
public float gravity = -10f;
public float jumpHeight = 2f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
#if ENABLE_INPUT_SYSTEM
InputAction movement;
InputAction jump;
void Start()
{
movement = new InputAction("PlayerMovement", binding: "<Gamepad>/leftStick");
movement.AddCompositeBinding("Dpad")
.With("Up", "<Keyboard>/w")
.With("Up", "<Keyboard>/upArrow")
.With("Down", "<Keyboard>/s")
.With("Down", "<Keyboard>/downArrow")
.With("Left", "<Keyboard>/a")
.With("Left", "<Keyboard>/leftArrow")
.With("Right", "<Keyboard>/d")
.With("Right", "<Keyboard>/rightArrow");
jump = new InputAction("PlayerJump", binding: "<Gamepad>/a");
jump.AddBinding("<Keyboard>/space");
movement.Enable();
jump.Enable();
}
#endif
// Update is called once per frame
void Update()
{
float x;
float z;
bool jumpPressed = false;
#if ENABLE_INPUT_SYSTEM
var delta = movement.ReadValue<Vector2>();
x = delta.x;
z = delta.y;
jumpPressed = Mathf.Approximately(jump.ReadValue<float>(), 1);
#else
x = Input.GetAxis("Horizontal");
z = Input.GetAxis("Vertical");
jumpPressed = Input.GetButtonDown("Jump");
#endif
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if(jumpPressed && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment