Forum begins after the advertisement:
[Part 3] “Knife” (arrow) weapon orientation issue
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 3] “Knife” (arrow) weapon orientation issue
- This topic has 5 replies, 3 voices, and was last updated 2 weeks, 1 day ago by
Terence.
-
AuthorPosts
-
April 3, 2026 at 3:42 pm #19315::
Instead of a knife i made an arrow, it has the same orientation as the knife (pointed to top right) and even when i change the prefab to be -45, it still comes out pointed to the top right, i’ve tried it with other prefabs and they all do the same thing.
please help, here’s my Arrow (knife) Controller code requested by Terresquall the code is a 1 to 1 of the KnifeController code, but i just replaced all the words with knife with arrow, the code seems to work.
using UnityEngine; public class ArrowController : WeaponController { // Start is called once before the first execution of Update after the MonoBehaviour is created protected override void Start() { base.Start(); } protected override void Attack() { base.Attack(); GameObject spawnedArrow = Instantiate(prefab, transform.position, Quaternion.identity); // Set the position of the spawned arrow to the position of the weapon spawnedArrow.transform.position = transform.position; spawnedArrow.GetComponent<ArrowBehaviour>().DirectionChecker(pm.moveDir); } }View post on imgur.com
April 3, 2026 at 4:30 pm #19316::Hello Tristin! I see the issue – you’re passing
pm.moveDirtoDirectionChecker(), but according to the tutorial, you should be passingpm.lastMovedVectorinstead.Change this line:
spawnedArrow.GetComponent().DirectionChecker(pm.moveDir);To this:
spawnedArrow.GetComponent().DirectionChecker(pm.lastMovedVector);The
lastMovedVectortracks the last direction the player moved, which is what theDirectionChecker()method expects to properly orient your arrow in all 8 directions.April 3, 2026 at 4:42 pm #19317April 3, 2026 at 10:26 pm #19318::You’ve got the right idea with rotating the prefab, but instead of rotating the prefab, you need to put the arrow as a child GameObject and rotate that child GameObject instead. This is because the code sets the rotation of your prefab when it spawns:
GameObject spawnedArrow = Instantiate(prefab, transform.position, Quaternion.identity);So if you set the prefab directly, the rotation will be overriden by your script.
Let me know if this clarifies.
April 3, 2026 at 10:40 pm #19319::a child object of what though? sorry if that’s a stupid question, i’m still learning and i know it can be annoying to constantly help people that don’t know what they’re doing so i’m sorry.
EDIT, i made it a child object to the player, probably not correct because it still does the same thing. i feel a little stupid haha.
has upvoted this post. April 4, 2026 at 12:56 am #19320::i fixed the code with
if (dir == Vector3.zero) dir = Vector3.right; direction = dir.normalized; float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg; // FIX: changes rotation based on rotation position transform.rotation = Quaternion.Euler(0, 0, angle - 45f);now i can just change what my angle is at
has upvoted this post. April 5, 2026 at 12:10 pm #19322::i fixed the code with
if (dir == Vector3.zero) dir = Vector3.right;
direction = dir.normalized; float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg; // FIX: changes rotation based on rotation position transform.rotation = Quaternion.Euler(0, 0, angle - 45f);now i can just change what my angle is at
This works too! It’s a brilliant fix. I would avoid doing that though, because if you plan to reuse the script for other weapons (say other kinds of arrow weapons), they will all have to have sprites that are tilted 45 degrees. But if all your sprites are designed that way, there is no harm just directly adjusting the angle in your code.
a child object of what though? sorry if that’s a stupid question, i’m still learning and i know it can be annoying to constantly help people that don’t know what they’re doing so i’m sorry.
EDIT, i made it a child object to the player, probably not correct because it still does the same thing. i feel a little stupid haha.
Not a stupid question at all. What I mean was something like this:
Prefab Root (empty GameObject) -> Sprite GameObject (sprite renderer goes here, you rotate this child object)
This works because your code sets the rotation of the Prefab Root. If you change the rotation of the Prefab Root instead of the Sprite GameObject, the code overwrites the rotation of the Prefab Root when it spawns anyway, so whatever rotation you set in the prefab root is overwritten. If you set it in the Sprite GameObject, that would not be the case.
I hope this explains it clearer!
-
AuthorPosts
- You must be logged in to reply to this topic.