Move a RigidBody3D according to an angle :0
Asked Answered
A

2

0

Hi all! I'm working on my first Godot game, and I'm trying to achieve a super specific thing I hope you can help me out with 🙂

Here's the idea; I have a RigidBody3D that can move around my 3D environment no problem. I've got a placeholder script that lets it move with arrow keys, but what I really want is to be able to steer by moving the mouse and move it in the specified direction by clicking.

I made a little doodle to demonstrate what I mean.

I've got an idea in my mind of how I want to make that work; I've already got some code in the player script that grabs the mouse's angle from the center of the screen according to a 2D pointer. What I want to do is take that value and apply force to the RigidBody in that direction while the left mouse button is pressed, effectively steering it in the direction of the mouse. The tiny problem with that is I have no idea where to start!

A few notes:
-The body itself doesn't need to rotate or anything (unless, of course, that would send it in the direction I want it to go!).
-The reason I'm using RigidBody for the character is I like the way it skids and slides around on the ground. I'd prefer to keep it in RigidBody, but if that's not possible I can switch the type and write a script to make it slide around manually XD
-I'm a beginner! I still don't understand how some stuff works at a fundamental level yet, so please be patient ^^

I'll drop the player.gd and pointer.gd scripts below so you can get a feel for how I've got things set up right now.
[upl-image-preview url=

Can anybody help me out with this? Let me know if I didn't give enough info to be able to help. It's been stumping me. Thank you!

Antonia answered 12/2 at 14:51 Comment(0)
V
0

If you haven't already read through the Physics introduction docs to better understand the different physics bodies and see which one will work best for your desired affect.

For example, if you used a CharacterBody3D, which is probably what you want, you would create some velocity in the direction of your angle.

This example, from the docs, shows you how you would update the velocity given some direction vector. Your direction vector would be derived from the angle you've calculated.

extends CharacterBody2D

var speed = 300

func get_input():
	var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
	velocity = input_dir * speed

func _physics_process(delta):
	get_input()
	move_and_collide(velocity * delta)
Vassar answered 21/2 at 4:3 Comment(0)
A
0

Hi! Still lookin for an answer on this one, I'm really uncertain what to do. All I need to know is how to move a RigidBody3D according to an angle; I can figure out the rest for myself. Plz respond if u think u can help!!!

Antonia answered 14/2 at 12:59 Comment(0)
V
0

If you haven't already read through the Physics introduction docs to better understand the different physics bodies and see which one will work best for your desired affect.

For example, if you used a CharacterBody3D, which is probably what you want, you would create some velocity in the direction of your angle.

This example, from the docs, shows you how you would update the velocity given some direction vector. Your direction vector would be derived from the angle you've calculated.

extends CharacterBody2D

var speed = 300

func get_input():
	var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
	velocity = input_dir * speed

func _physics_process(delta):
	get_input()
	move_and_collide(velocity * delta)
Vassar answered 21/2 at 4:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.