character moves better with camera and physics values have changed
This commit is contained in:
@@ -71,3 +71,5 @@ transform = Transform3D(0.994703, -0.00611287, -0.102607, 0.101106, 0.238088, 0.
|
|||||||
shadow_enabled = true
|
shadow_enabled = true
|
||||||
|
|
||||||
[node name="player" parent="." instance=ExtResource("1_1hi18")]
|
[node name="player" parent="." instance=ExtResource("1_1hi18")]
|
||||||
|
JumpVelocity = 5.5
|
||||||
|
gravity = 14.0
|
||||||
|
@@ -1,12 +1,12 @@
|
|||||||
using Godot;
|
using Godot;
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
|
||||||
|
|
||||||
public partial class player : CharacterBody3D
|
public partial class player : CharacterBody3D
|
||||||
{
|
{
|
||||||
|
public float gotDelta;
|
||||||
public const float Speed = 5.0f;
|
public const float Speed = 5.0f;
|
||||||
public const float JumpVelocity = 4.5f;
|
[Export] float JumpVelocity = 5.5f;
|
||||||
public float gravity = ProjectSettings.GetSetting("physics/3d/default_gravity").AsSingle();
|
[Export] float gravity = 14f;
|
||||||
[Export (PropertyHint.Range, "0.1,1.0")] float mouseSensitivity = 0.3f;
|
[Export (PropertyHint.Range, "0.1,1.0")] float mouseSensitivity = 0.3f;
|
||||||
[Export (PropertyHint.Range, "-90,0,1")] float minCamPitch = -90f;
|
[Export (PropertyHint.Range, "-90,0,1")] float minCamPitch = -90f;
|
||||||
[Export (PropertyHint.Range, "0,90,1")] float maxCamPitch = 90f;
|
[Export (PropertyHint.Range, "0,90,1")] float maxCamPitch = 90f;
|
||||||
@@ -14,31 +14,21 @@ public partial class player : CharacterBody3D
|
|||||||
public Camera3D camera;
|
public Camera3D camera;
|
||||||
public Vector3 camPosition;
|
public Vector3 camPosition;
|
||||||
public Vector3 playerResetPosition;
|
public Vector3 playerResetPosition;
|
||||||
|
public Vector3 direction;
|
||||||
//TODO: Add camera max rotation on ground (Preventing turning camera to upside down)
|
//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()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
cameraCenter = GetNode<Marker3D>("camera_center");
|
cameraCenter = GetNode<Marker3D>("camera_center");
|
||||||
camera = GetNode<Camera3D>("camera_center/spring_arm/camera");
|
camera = GetNode<Camera3D>("camera_center/spring_arm/camera");
|
||||||
mouseSensitivity = mouseSensitivity * 0.005f;
|
mouseSensitivity = mouseSensitivity / 2;
|
||||||
Input.MouseMode = Input.MouseModeEnum.Captured;
|
Input.MouseMode = Input.MouseModeEnum.Captured;
|
||||||
}
|
}
|
||||||
public override void _Process(double delta)
|
public override void _Process(double delta)
|
||||||
{
|
{
|
||||||
|
gotDelta = (float)delta;
|
||||||
if (Input.IsActionJustPressed("uncapture_mouse")) Input.MouseMode = Input.MouseModeEnum.Visible;
|
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.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)
|
public override void _PhysicsProcess(double delta)
|
||||||
{
|
{
|
||||||
@@ -51,11 +41,18 @@ public partial class player : CharacterBody3D
|
|||||||
velocity.y = JumpVelocity;
|
velocity.y = JumpVelocity;
|
||||||
|
|
||||||
Vector2 inputDir = Input.GetVector("move_left", "move_right", "move_forward", "move_backward");
|
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)
|
if (direction != Vector3.Zero)
|
||||||
{
|
{
|
||||||
velocity.x = direction.x * Speed;
|
velocity.x = direction.x * Speed;
|
||||||
velocity.z = direction.z * 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
|
else
|
||||||
{
|
{
|
||||||
@@ -73,12 +70,12 @@ public partial class player : CharacterBody3D
|
|||||||
if(@event is InputEventMouseMotion motionEvent && Input.MouseMode == Input.MouseModeEnum.Captured)
|
if(@event is InputEventMouseMotion motionEvent && Input.MouseMode == Input.MouseModeEnum.Captured)
|
||||||
{
|
{
|
||||||
Vector3 generalRot = Rotation;
|
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;
|
Rotation = generalRot;
|
||||||
generalRot = cameraCenter.Rotation;
|
generalRot = cameraCenter.Rotation;
|
||||||
if (!Input.IsActionPressed("move_forward")) generalRot.y -= motionEvent.Relative.x * mouseSensitivity;
|
if (direction == Vector3.Zero) generalRot.y -= motionEvent.Relative.x * mouseSensitivity * (float)gotDelta; ;
|
||||||
generalRot.x -= motionEvent.Relative.y * mouseSensitivity;
|
generalRot.x -= motionEvent.Relative.y * mouseSensitivity * (float)gotDelta; ;
|
||||||
generalRot.x = Mathf.Clamp(generalRot.x, minCamPitch, maxCamPitch);
|
generalRot.x = Mathf.Clamp(generalRot.x, minCamPitch, maxCamPitch * (float)gotDelta);
|
||||||
cameraCenter.Rotation = generalRot;
|
cameraCenter.Rotation = generalRot;
|
||||||
generalRot = cameraCenter.Rotation;
|
generalRot = cameraCenter.Rotation;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user