|
|
|
|
Reply From: |
coBearCoding |
Hey! answering to your question in Godot C#, you need to instance your scene before you instanciate a object from it. It is suggested to give its own logic to the bullets for actual movement of them. Basicly in the example im calling the script generated and called Bullet.cs
Using your Firing Example it would be like this:
Declaration outside Ready() or Physcis_Process
private PackedScene _Bullet = (PackedScene)GD.Load("res://Bullet.tscn");
In actual FireWeapon:
private void FireWeapon()
{
firingDelay--;
if ((Input.IsActionJustPressed("moveShoot")) && (firingDelay < 0))
{
var newBullet = (Bullet)_Bullet.Instance();
GetParent().AddChild(newBullet);
}
}
So im using the GetParent() function to get the object that’s executing the action of firing, then with it I can set its position to it, adding it as a child. You can refer this explanation for a more detailed comparison on the official Godot Docs
You can look at my work with the Godot Engine at my Youtube Channel: bearcoding
Hope this helps you! and continue your good work with the Godot Engine ! :D!
As an addition since your new to the Godot Engine, I’ll suggest learning GDScript for you, C# is great… but in Godot is still being implemented, so it’s not as compatible as it is with GDScript and often you will have to look for a workaround of a part that GDScript does with a very simple context and syntax, if performance is an issue for you, feel good to know that C# and GDScript perform really well, and use cases of them are really a niche decision that will supply both programming languages preferences and project needs :3
coBearCoding | 2020-04-29 04:42
Ok, I did that and I no longer get an error. However, I added the weapon as a child node of my player, and now my player glitches up and down, his movement is occasionally sporadic and fast, and he gets stuck on the platforms. Any idea why that is happening?
Hi, since I have no idea of how you’ve coded the whole logic, I’m not sure of the error you’re getting ^^, but well you should try to split the logic for each actor, you can put the logic for turning (moving o jumping) into the player and leave the weapon without any of it, since it is a child of the player it will inherit its position, for the bullet you would want to put it as a child of your weapon instead, so it goes out of the muzzle (if you have a gun) inside the gun you can create an empty 2d Node for being specific a position2D right in the top of the muzzle so you can spawn the bullet there it will inherit both position of the muzzle and at the same time the weapon will get the position of the player ^^
coBearCoding | 2020-05-12 01:24