I just started working on a small, single player 2D shooter game. And obviously, it has collectibles like guns. I created a pickable gun, which is a pistol, and coded it so whenever the player touches it, it teleports to the players hand. For some reason, it isn't working
I tried out different coordinates, but it still teleports away from the player (currently 0,0 for testing). Here's the code:
using Godot;
using System;
public class Pistol : Area2D
{
[Export] public int speed = 200;
public Vector2 playerPosition;
public override void _Ready()
{
var detect = new Character();
playerPosition = detect.characterPosition;
initialPosition = this.Position;
}
public override void _PhysicsProcess(float delta)
{
//* Making the pistol move
var motion = new Vector2();
motion.x = Input.GetActionStrength("ui_left") - Input.GetActionStrength("ui_right");
motion.y = Input.GetActionStrength("ui_up") - Input.GetActionStrength("ui_down");
if (Input.IsActionPressed("ui_right") || Input.IsActionPressed("ui_left"))
{
MoveLocalX(motion.x * speed * delta);
}
if (Input.IsActionPressed("ui_up") || Input.IsActionPressed("ui_down"))
{
MoveLocalY(motion.y * speed * delta);
}
}
private void _on_Pistol_body_entered(object body)
{
this.Position = new Vector2(0, 0);
}
}
Update: the sprite was dislocated from the Area2D pistol.
Ignoring why you want to teleport the collectible, and why the collectible moves on input, and… It is wtf there, but ignoring all that…
The Position
is relative to the parent. So Vector2(0, 0)
is the position of the parent. You want to work with GlobalPosition
s instead.
Something like this:
var target = body as Spatial;
if (Object.IsInstanceValid(target))
{
this.GlobalPosition = target.GlobalPosition;
}
You can be more specific and use the player character class (assuming it is called PlayerCharacter
here):
var target = body as PlayerCharacter;
if (Object.IsInstanceValid(target))
{
this.GlobalPosition = target.GlobalPosition;
}
You probably want to add a Position2D
as child of the player so you can tweak the position for the gun (assuming it is called "GunPosition"
here). So you would do something like this:
var target = body as PlayerCharacter;
if (Object.IsInstanceValid(target))
{
this.GlobalPosition = target.GetNode<Position2D>("GunPosition").GlobalPosition;
}