Другие языки программирования и технологии

Как сделать поворот 2д персонажа в godot 4.0.1? Если мышка на левой половине экрана то персонаж поворачиваются влево

Как сделать поворот 2д персонажа в godot 4.0.1?
Если мышка на левой половине экрана то персонаж поворачиваются влево и аналогично справа
ЗА РАНЕЕ СПАСИБО
Ммм... смотри, создай переменную, назови ее как-нибудь, допустим rotation. Объявляешь так
 var rotation = get_global_mouse_position() - position 
Первой функцией мы получаем глобальный позишн мыши, от него вычитаем позишн игрока
Далее сделай проверку, что если rotation.x больше нуля, то flip_h будет равно false, а если меньше нуля, то true.
Это должно работать
Сергей Ковалёв
Сергей Ковалёв
8 590
Лучший ответ
Андрей Пчелин Спасибо за чекну как я до этого не додумался
To rotate a 2D character in Godot following the mouse cursor, you can use the following steps:
1. Add a sprite node to your scene and set its texture to your character's image.
2. Add a script to the sprite node by clicking on the node and selecting "Attach Script" in the Inspector panel.
3. In the script, add a function to handle the mouse movement:
 func _process(delta):  
var mouse_pos = get_global_mouse_position()
var angle = (mouse_pos - position).angle()
rotation = angle
4. In the above code, `get_global_mouse_position()` returns the position of the mouse cursor in the global coordinate system, while `position` is the position of the sprite node in the scene.
5. The `angle()` function calculates the angle between the vector from the sprite's position to the mouse position and the x-axis.
6. Set the `rotation` property of the sprite node to the calculated angle. This will rotate the sprite to face the mouse cursor.
7. Save the script and test the scene by running it. The sprite should rotate to face the mouse cursor as you move it around the screen.
Максим Саушев
Максим Саушев
1 396

Похожие вопросы