Hi,
Please bare with me while I try to explain my issue, and also please note i’ve spend the past week and a half teaching myself unity so be gently with your replies
My goal is to create a 2D topdown shooter, i have a player game object which has an animated sprite attached to it. As part of my playermovement code, i’m flipping the character on it’s X axis depending on which way the player is moving. This works fine.
I have a seperate “Weapon” GameObject which has a sprite attached (no animation here, just an object with a sprite renderer). My Intent later down the line is to add additional weapons.
If the player rotates the right thumbstick on a gamepad, the weapon “orbits” around the player to reflect the aim direction, this works 100% as I want.
So the problem I have is how to attach the weapon to the player. I’ve tried two different approaches, both of which come with their own problems:
- ClickOps the weapon game object onto the player object (so the weapon becomes a child of the Player)
While this works as far as making the weapon move around with the player, the problem I now have is if the player changes direction (ie, moves to the left) the weapon is then flipped and the movement in that direction is inverted. This could be solved potentially via code but honestly i’ve spend hours on this today and my brain really is starting to ache trying to understand the math involved in doing the opposite of what i’ve already spent a day trying to achive in getting the weapon to rotate around the player)
- Leave the player and weapon as two seperate game objects, and inside the Weapon code (PlayerWeaponSlot.cs ← with intent to hold different weapons) update the positionof the weapon game object to match that of the player.
I had very strange experience with this where if i move around using the left thumbstick, all seems okay, meaning the weapon “attaches” to the player". However as soon as i aim (with right thumbstick) the weapon resets position to 0, 0, 0 and proceeds to rotate on the spot.
Ideally i would prefer assistance with number 2 since i feel in the long run it will enable better handling of multiple weapons down the line. Here’s some code the show how i’m getting the above results.
private void Update()
{
// Get the player position:
playerPosition = playerRigidBody.transform.position;
// Get the player Rotation:
playerRotation = playerRigidBody.transform.rotation;
// Attach the weapon to the player:
weaponSprite.transform.position = new Vector3(playerPosition.x + weaponDistanceFromPlayer, playerPosition.y, playerPosition.z); // playerPosition;
// Get the players aim direction
aimDirection = playerAim.ReadValue<Vector2>();
AimWeapon();
}
The line to note here is where i’m setting weaponSprite.transform.position. Also note that weaponDistanceFromPlayer is a float representing the distance from the orbits center (this basically gives the weapon distance from the player so it “orbits”)
So the way i understand it, since Update() is triggered every frame, surely my weaponSpride.transform.position should always match that of my playerPosition regardless of if i’m moving or aiming?
For context, here’s my aim method:
private void AimWeapon()
{
if (aimDirection.y != 0 || aimDirection.x != 0)
{
// Flip the weapon into the direction it's aiming
if (WeaponIsFacingRight())
{
weaponSprite.flipX = true;
weaponSprite.flipY = false;
}
else
{
weaponSprite.flipX = true;
weaponSprite.flipY = true;
}
// Get the x and y aim direction, times by the tick rate and player move speed
float x = aimDirection.x * Time.deltaTime * moveSpeed;
float y = aimDirection.y * Time.deltaTime * moveSpeed;
//convert the input into an angle in radians, and convert that into degrees
rads = Mathf.Atan2(y, x);
float degrees = rads * Mathf.Rad2Deg;
float weaponPositionX = Mathf.Cos(rads) * weaponDistanceFromPlayer;
float weaponPositionY = Mathf.Sin(rads) * weaponDistanceFromPlayer;
// Transform the weapon
weaponSprite.transform.localPosition = new Vector3(weaponPositionX, weaponPositionY, 0);
weaponSprite.transform.localEulerAngles = new Vector3(0, 0, degrees);
}
}
I really appreciate any assistance with this, if theres any more information i can provide or any questions i can answer please let me know
Thanks!