character moves better with camera and physics values have changed
This commit is contained in:
		| @@ -1,12 +1,12 @@ | ||||
| using Godot; | ||||
| using System; | ||||
| using System.Diagnostics; | ||||
|  | ||||
| public partial class player : CharacterBody3D | ||||
| { | ||||
| 	public float gotDelta; | ||||
| 	public const float Speed = 5.0f; | ||||
| 	public const float JumpVelocity = 4.5f; | ||||
| 	public float gravity = ProjectSettings.GetSetting("physics/3d/default_gravity").AsSingle(); | ||||
|     [Export] float JumpVelocity = 5.5f; | ||||
| 	[Export] float gravity = 14f; | ||||
| 	[Export (PropertyHint.Range, "0.1,1.0")] float mouseSensitivity = 0.3f; | ||||
| 	[Export (PropertyHint.Range, "-90,0,1")] float minCamPitch = -90f; | ||||
| 	[Export (PropertyHint.Range, "0,90,1")] float maxCamPitch = 90f; | ||||
| @@ -14,31 +14,21 @@ public partial class player : CharacterBody3D | ||||
| 	public Camera3D camera; | ||||
| 	public Vector3 camPosition; | ||||
| 	public Vector3 playerResetPosition; | ||||
| 	public Vector3 direction; | ||||
| 	//TODO: Add camera max rotation on ground (Preventing turning camera to upside down) | ||||
| 	//TODO: rotate player "model" better in walking direction | ||||
|     public override void _Ready() | ||||
| 	{ | ||||
|         cameraCenter = GetNode<Marker3D>("camera_center"); | ||||
|         camera = GetNode<Camera3D>("camera_center/spring_arm/camera"); | ||||
|         mouseSensitivity = mouseSensitivity * 0.005f; | ||||
|         Input.MouseMode = Input.MouseModeEnum.Captured; | ||||
| 		mouseSensitivity = mouseSensitivity / 2; | ||||
| 		Input.MouseMode = Input.MouseModeEnum.Captured; | ||||
|     } | ||||
| 	public override void _Process(double delta) | ||||
| 	{ | ||||
| 		gotDelta = (float)delta; | ||||
|         if (Input.IsActionJustPressed("uncapture_mouse")) Input.MouseMode = Input.MouseModeEnum.Visible; | ||||
|         if (Input.IsMouseButtonPressed(MouseButton.Left)) Input.MouseMode = Input.MouseModeEnum.Captured;//just uncapture to also disable player and camera movement | ||||
|         if (Input.IsActionJustPressed("move_forward") || Input.IsActionJustPressed("move_forward") && Input.MouseMode == Input.MouseModeEnum.Captured) | ||||
| 		{ | ||||
| 			/*TODO: make this for all movement input not just forward | ||||
| 			and model should still walk sideways and to the camera then.  | ||||
|  | ||||
| 			Would work with just using all inputs | ||||
| 			but model doesnt rotate how it should*/ | ||||
| 			playerResetPosition = Rotation;  | ||||
|             camPosition = cameraCenter.Rotation; | ||||
| 			playerResetPosition.y = camPosition.y * -1f; | ||||
| 			Rotation -= playerResetPosition; | ||||
| 			cameraCenter.Rotation = new Vector3 (camPosition.x,0,0); | ||||
| 		} | ||||
|     } | ||||
| 	public override void _PhysicsProcess(double delta) | ||||
| 	{ | ||||
| @@ -51,17 +41,24 @@ public partial class player : CharacterBody3D | ||||
| 			velocity.y = JumpVelocity; | ||||
|  | ||||
| 		Vector2 inputDir = Input.GetVector("move_left", "move_right", "move_forward", "move_backward"); | ||||
| 		Vector3 direction = (Transform.basis * new Vector3(inputDir.x, 0, inputDir.y)).Normalized(); | ||||
| 		direction = (Transform.basis * new Vector3(inputDir.x, 0, inputDir.y)).Normalized(); | ||||
| 		if (direction != Vector3.Zero) | ||||
| 		{ | ||||
| 			velocity.x = direction.x * Speed; | ||||
| 			velocity.z = direction.z * Speed; | ||||
| 		} | ||||
|  | ||||
|             //checks for movement and adjusts the character direction with camera roatation | ||||
|             playerResetPosition = Rotation; | ||||
|             camPosition = cameraCenter.Rotation; | ||||
|             playerResetPosition.y = camPosition.y * -1f; | ||||
|             Rotation -= playerResetPosition; | ||||
|             cameraCenter.Rotation = new Vector3(camPosition.x, 0, 0); | ||||
|         } | ||||
| 		else | ||||
| 		{ | ||||
| 			velocity.x = Mathf.MoveToward(Velocity.x, 0, Speed); | ||||
| 			velocity.z = Mathf.MoveToward(Velocity.z, 0, Speed); | ||||
| 		} | ||||
|         } | ||||
| 		if(Input.MouseMode == Input.MouseModeEnum.Captured) | ||||
| 		{ | ||||
| 			Velocity = velocity; | ||||
| @@ -73,12 +70,12 @@ public partial class player : CharacterBody3D | ||||
| 		if(@event is InputEventMouseMotion motionEvent && Input.MouseMode == Input.MouseModeEnum.Captured) | ||||
| 		{ | ||||
| 			Vector3 generalRot = Rotation; | ||||
|             if (Input.IsActionPressed("move_forward")) generalRot.y -= motionEvent.Relative.x * mouseSensitivity; | ||||
|             if (direction != Vector3.Zero) generalRot.y -= motionEvent.Relative.x * mouseSensitivity * (float)gotDelta; ; | ||||
|             Rotation = generalRot; | ||||
|             generalRot = cameraCenter.Rotation; | ||||
|             if (!Input.IsActionPressed("move_forward")) generalRot.y -= motionEvent.Relative.x * mouseSensitivity; | ||||
|             generalRot.x -= motionEvent.Relative.y * mouseSensitivity; | ||||
|             generalRot.x = Mathf.Clamp(generalRot.x, minCamPitch, maxCamPitch); | ||||
|             if (direction == Vector3.Zero) generalRot.y -= motionEvent.Relative.x * mouseSensitivity * (float)gotDelta; ; | ||||
|             generalRot.x -= motionEvent.Relative.y * mouseSensitivity * (float)gotDelta; ; | ||||
|             generalRot.x = Mathf.Clamp(generalRot.x, minCamPitch, maxCamPitch * (float)gotDelta); | ||||
| 			cameraCenter.Rotation = generalRot; | ||||
|             generalRot = cameraCenter.Rotation; | ||||
|         } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user