When i throw it closer to the player it immediately starts going up, but when i try to throw it farther away the grenade seems to work for a couple of seconds but then starts going up. This is my code:

if (Input.IsActionJustPressed("G"))
{
var grenadeScene = ResourceLoader.Load<PackedScene>("res://Scenes/grenade.tscn");
var grenadeInstance = grenadeScene.Instantiate<RigidBody3D>();

GetNode<Node3D>("/root/World").AddChild(grenadeInstance);
		
var forward = -camera.GlobalTransform.Basis.Z;
		
grenadeInstance.GlobalTransform = camera.GlobalTransform.Translated(forward * 2);
grenadeInstance.LinearVelocity = new Vector3(forward.X * 20, forward.Y * 10, forward.Z * 20);

}
The grenade is a RigidBody3D.

  • belgian_street_lamp and Jesusemora replied to this.
  • TheEpicCookie The grenade still goes up and only goes to one direction.

    what does the grenade look like?
    it could be a glitch with multiple rigidbodies colliding with each other:

    also check the physics layers, make sure the grenade is on a different one than the player and they don't interact.

    TheEpicCookie only goes to one direction.

    TheEpicCookie grenadeInstance.ApplyCentralImpulse((GlobalTransform.Basis * Vector3.Forward) * 20.0f);

    global_transform.basis is of the current node, which is not the one you are using to determine direction

    TheEpicCookie camera.GlobalRotation;

    it should probably be something like:
    camera.global_transform.basis

    TheEpicCookie The grenade still goes up

    are there any special settings for the rigidbody of the grenade?

    also, I can't open your project, it is not a valid project.

    TheEpicCookie you can't change the position of a rigidbody. read the docs.

    https://docs.godotengine.org/en/stable/classes/class_rigidbody3d.html

    Note: Changing the 3D transform or linear_velocity of a RigidBody3D very often may lead to some unpredictable behaviors. If you need to directly affect the body, prefer _integrate_forces as it allows you to directly access the physics state.

    you have to use apply_force and apply_impulse

      Can you create a Minimal Reproduction Project and upload it here? I can't run a Godot C# project, but someone else here can probably use it to determine the problem.

      Is that a complete project? There's no project.godot file, and Player.cs is one byte.

        TheEpicCookie did you stop manually changing velocity?

        I very recently coded a grenade launcher:
        1 - changing global_position is okay when instantiating the node
        2 - make sure rotation is global, as quaternions and basis are local.
        3 - rotate the apply_central_impulse by a global_basis to aim the grenade in the correct direction
        4 - one time forces in the spawn script. constant acceleration on the grenade script.

        func spawn_grenade(proj : PackedScene) -> void:
        	var n_p : RigidBody3D = proj.instantiate()
        	get_tree().root.add_child(n_p)
        	n_p.global_position = global_position
        	n_p.global_rotation = global_rotation
        	n_p.apply_central_impulse((global_basis * Vector3.FORWARD) * 20.0)

          Jesusemora I made it like this: var grenadeScene = ResourceLoader.Load<PackedScene>("res://grenade.tscn");
          var grenadeInstance = grenadeScene.Instantiate<RigidBody3D>();

          		GetNode<Node3D>("/root/World").AddChild(grenadeInstance);
          		
          		grenadeInstance.GlobalPosition = camera.GlobalPosition;
          		grenadeInstance.GlobalRotation = camera.GlobalRotation;
          		grenadeInstance.ApplyCentralImpulse((GlobalTransform.Basis * Vector3.Forward) * 20.0f);

          The grenade still goes up and only goes to one direction.

            TheEpicCookie The grenade still goes up and only goes to one direction.

            what does the grenade look like?
            it could be a glitch with multiple rigidbodies colliding with each other:

            also check the physics layers, make sure the grenade is on a different one than the player and they don't interact.

            TheEpicCookie only goes to one direction.

            TheEpicCookie grenadeInstance.ApplyCentralImpulse((GlobalTransform.Basis * Vector3.Forward) * 20.0f);

            global_transform.basis is of the current node, which is not the one you are using to determine direction

            TheEpicCookie camera.GlobalRotation;

            it should probably be something like:
            camera.global_transform.basis

            TheEpicCookie The grenade still goes up

            are there any special settings for the rigidbody of the grenade?

            also, I can't open your project, it is not a valid project.

              Jesusemora I found out what was causing the grenade to go up, the staticbody in the grenade was the problem, and i changed the GlobalTransform.Basis to camera.GlobalTransform.Basis and it works! Thanks!