How do I make a sword orbit around the player 90 degrees and back?

I’m new to Unity and C# and I think I’m biting off more than I can chew. I’ve been searching for hours for a solution to no avail. Basically I have my character holding a sword backwards and behind it’s back. I have code set up to swing the sword on its own Y axis but I need it to orbit the player’s Y axis during the animation as well. Any help would be greatly appreciated!

I haven’t seen your code, so this won’t be tailored to your case.

I’ll show an example of how to make an item orbit a character. The script only affects the item’s position, not rotation because you wrote “I have code set up to swing the sword on its own Y axis”.

Create an OrbitController script and attach it to a character object.

Drag the Sword object onto the itemTransform field.

Place the Helper class in its own CS file if you wish to use the method in multipe scripts.

If not then simply move the method to the OrbitController class and remove all instances of the word “static”.

First test: When you run the game the Sword should automatically orbit the character at 90 degrees per second. Try moving the character to make sure it follows him.

This is just a test, of course. Remove the line of code that does it when you’re done testing.

Second test: After disabling the aforementioned test code, try changing the orbitAngleAroundCharacterAxisY field in the Inspector while the game is running.

You’ll have to take it from here and try to make your script interact with this code or merge the two codes as you please.

If you need more help, I would prefer that you show the code you have now or at least a simplified version of it for testing purposes.

OrbitController class:

public class OrbitController : MonoBehaviour
{
	public Transform itemTransform;
	public float orbitRadius = 1;
	public float orbitAngleAroundCharacterAxisY;

	private void Update()
	{
		if (itemTransform != null && Mathf.Abs(orbitRadius) > 0)
		{
			Transform characterTransform = this.transform; // For readability's sake
			Helper.OrbitItemAroundCharacter (characterTransform, itemTransform, orbitRadius, orbitAngleAroundCharacterAxisY);

			// For testing purposes only:
			// The item orbits the character at 90 degrees per second
			orbitAngleAroundCharacterAxisY = 90 * Time.time;
		}
	}
}

Helper class:

public static class Helper
{
	public static void OrbitItemAroundCharacter (Transform characterTransform, Transform itemTransform, float orbitRadius, float orbitAngleAroundCharacterAxisY)
	{
		itemTransform.position =
			  characterTransform.position
			+ characterTransform.rotation
			* Quaternion.Euler (0, orbitAngleAroundCharacterAxisY, 0)
			* Vector3.forward
			* orbitRadius;
	}
}