Topic was automatically imported from the old Question2Answer platform.
Asked By
Yozhik
I have a playercontroller 2d in top-down view game, where the players ship follows the cursor. When I increase cursor speed it rotates with it instantly. And I want players to feel some weight, so make the ship rotating with some delay. How can I do it? Would be grateful for help. Controller code below. (Player.gd)
extends KinematicBody2D
export var move_speed = 250
export var stop_distance = 20
var playercoord = Vector2()
func _process(delta):
_look_at_mouse()
_move_to_mouse()
if G.player_alive == 1:
playercoord = get_node("/root/MainScene/Player").get_position()
func _look_at_mouse():
look_at(get_global_mouse_position())
rotation_degrees = rotation_degrees + 90
func _move_to_mouse():
if position.distance_to(get_global_mouse_position()) > stop_distance:
var direction = get_global_mouse_position() - position
move_and_slide(direction)
ALSO, I’ve got enemies, and I want them to have weight slightly bigger than player, to have him feel he’s a bit faster and subtle than the enemies. It’s part of enemies controller code:
it’s in the phys-process:
if player:
if G.player_alive == 1:
var direction = (player.position - position).normalized()
move_and_slide(direction * MOVE_SPEED)
pass
func _look_at_player():
look_at(playercoord)
rotation_degrees = rotation_degrees + 90
In other words You want to interpolate angle in time. There are few functions for that, like move_toward() , lerp(), lerp_angle(). In those functions You set starting angle and desired angle as first 2 arguments, and interpolation value as second. If You use delta as interpolation value it will get close to angle with constant speed. You can use non-constant interpolation values for easing in or out. Check those functions in script documentation.
Thanks for assistance. I noticed, that these functions use float values, and I move and rotate objects by using coordinates and Vector values.
var rotation_speed = PI * 1.5 # PI = 180 degrees
func _look_at_mouse(delta):
var v = get_global_mouse_position() - global_position
var angle = v.angle()
var r = global_rotation
var angle_delta = rotation_speed * delta
angle = lerp_angle(r, angle + 1.57, 1.0)
angle = clamp(angle, r - angle_delta, r + angle_delta)
global_rotation = angle
Not quite Yourself mate, that is rude. You take someones time and You are expected to credit him.
Inces | 2021-10-19 17:01
I appreciate everyone who helps such guys as me, because every one from time to time needs help, but in search of an answer, I tried different things, and tooked code from different people line by line. In the end, I modified it to meet the task by myself. So I can call this “my code” for reasons above.