Physics Gravity Gone Wonkers

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By BlipTheBloop

(Edit Begin)
Godot 3.0 Stable

Linux Ubuntu 18.10

Player scene consists of kinematic body with child sphere collision around body(mentioned later)
Child Spacial “Head” Mesh and collision Sphere + camera node
Child Spacial “Body” Mesh and collision Capsule
Child Spacial “Left Arm” Mesh and Collision Sphere
Child Spacial “Right Arm” Mesh and Collision Sphere
Child Spacial “Left Leg” Mesh and Collision Capsule
Child Spacial “Right Leg” Mesh and Collision Capsule

Main scene consists of a (200, 0.5, 200) Mesh Square with Collision as Main Floor
And various other objects created as meshes with collisions as a basic test map.

Root Tree Kinematic Body is the only one with a Script (Script Below)
(Edit End)

I attempted to make a basic FPS with camera and player position facing mouse position, however when I run the main scene my character goes balistic and gravity pulls it up left then down right and multiple ways even if I don’t move the mouse position or hit any buttons. I also noticed that the camera ignores my instructions not to go past >90 and <-90. Does anybody know where I went wrong or have any suggestions? Here is my Player Script below.

extends KinematicBody

var camera_angle = 0
var mouse_sensitivity = 0.3
var velocity = Vector3(0, 0, 0)
var direction = Vector3(0, 0, 0)

var gravity = -9.8 
const MAX_SPEED = 20
const ACCEL = 2
const DEACCEL = 6

var jump_height = 15

func _ready():


func _physics_process(delta):
	direction = Vector3()
	var aim = $Head.get_global_transform().basis
	if Input.is_action_pressed("ui_up"):
		direction -= aim.z
	if Input.is_action_pressed("ui_down"):
		direction += aim.z
	if Input.is_action_pressed("ui_left"):
		direction -= aim.x
	if Input.is_action_pressed("ui_right"):
		direction += aim.x
	direction = direction.normalized()
	velocity.y += gravity * delta
	var temp_velocity = velocity
	temp_velocity.y = 0
	var speed
	if Input.is_action_pressed("move_sprint"):
		speed = MAX_SPEED
	var target = direction * speed
	var acceleration
	if > 0:
		acceleration = ACCEL
		acceleration = DEACCEL
	velocity = velocity.linear_interpolate(target, acceleration * delta)
	velocity.x = temp_velocity.x
	velocity.z = temp_velocity.z
	velocity = move_and_slide(velocity, Vector3(0, 1, 0))

func _input(event):
	if event is InputEventMouseMotion:
		$Head.rotate_y(deg2rad(-event.relative.x * mouse_sensitivity))
		var change = event.relative.y * mouse_sensitivity
		if change + camera_angle <90 and change + camera_angle > -90:
			$Head.rotate_x(deg2rad(-event.relative.y * mouse_sensitivity))

I have fixed the code tags, remember to select your code and press the brackets button to have it formatted, and look at the preview to see if is correct too when writing or nobody will understand.

eons | 2019-01-27 00:22

Are you using the Beta version? There are a few issues about physics, I opened this one about what you are describing.

davidoc | 2019-01-27 02:48

I am using a stable version of 3.0 and thank you for fixing my code format, sorry about that. Before adding movement relative to mouse position, gravity and collisions worked fine, but I had a seperate camera node control view by mouse position which caused directions to not be relative to the mouse position (that was using the camera script asset mentioned below). I switched to have kinematic body control all and camera set as current, hoping to resolve this but it gave more options. (This is what I was using prior ->) I have a script borrowed from assets for a camera view and it has movement for camera in script. I tried adding gravity and making my character a child of the camera asset, but that resulting in falling straight through the floors ignoring my collision shapes. So I went back to the drawing board and watched a lot of tutorials, and used the camera script asset as an idea to help guide me which landed me with the current code. However I don’t know if collisions work on this code due to it doesn’t go towards the ground and instead jerks upward in random directions without pushing buttons.

BlipTheBloop | 2019-01-27 05:39