Hey, I made this script and it shows no errors, I attached it to my drone modle but when I go into play mode and press WASD, it doesn’t move.
Any idea why?
Here´s the script:
import omni
import carb
from pxr import Gf, UsdPhysics
Update the path to your actual Rigidbody in the scene
rigid_body_path = “/World/Drone”
Get the stage and ensure the rigid body exists
stage = omni.usd.get_context().get_stage()
rigid_body_prim = stage.GetPrimAtPath(rigid_body_path)
if not rigid_body_prim.IsValid():
print(f"Error: No valid rigid body found at {rigid_body_path}")
else:
# Ensure the Rigidbody API is applied
rigid_body_api = UsdPhysics.RigidBodyAPI.Apply(rigid_body_prim)
# Define movement speed and initial velocity
speed = 500.0
velocity = Gf.Vec3f(0.0, 0.0, 0.0)
# Function to update velocity based on the current input state
def update_velocity():
omni.physx.get_physx_interface().set_rigid_body_linear_velocity(rigid_body_prim, velocity)
print(f"Velocity set to: {velocity}") # Debugging output
# Polling loop for input handling
def on_update(e):
global velocity
# Reset velocity each frame
velocity = Gf.Vec3f(0.0, 0.0, 0.0)
# Check input states and modify velocity accordingly
input_manager = carb.input.acquire_input_interface()
if input_manager.is_key_down(carb.input.KeyboardInput.W):
velocity[500] = -speed
if input_manager.is_key_down(carb.input.KeyboardInput.S):
velocity[500] = speed
if input_manager.is_key_down(carb.input.KeyboardInput.A):
velocity[-500] = -speed
if input_manager.is_key_down(carb.input.KeyboardInput.D):
velocity[-500] = speed
# Update the rigid body's velocity
update_velocity()
# Subscribe to the update loop
app = omni.kit.app.get_app()
app.get_update_event_stream().create_subscription_to_pop(on_update)
print("WASD controls are now active for the Rigidbody.")