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

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #19315
    Tristin Bartlett
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    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
    #19316
    Alp Apustaja
    Level 10
    Member
    Helpful?
    Up
    0
    ::

    Hello Tristin! I see the issue – you’re passing pm.moveDir to DirectionChecker(), but according to the tutorial, you should be passing pm.lastMovedVector instead.

    Change this line:

    spawnedArrow.GetComponent().DirectionChecker(pm.moveDir);

    To this:

    spawnedArrow.GetComponent().DirectionChecker(pm.lastMovedVector);

    The lastMovedVector tracks the last direction the player moved, which is what the DirectionChecker() method expects to properly orient your arrow in all 8 directions.

    #19317
    Tristin Bartlett
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    that is not what the tutorial says in the video or the forum posts. god i hate ai

    #19318
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    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.

    #19319
    Tristin Bartlett
    Level 3
    Participant
    Helpful?
    Up
    1
    ::

    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.
    #19320
    Tristin Bartlett
    Level 3
    Participant
    Helpful?
    Up
    1
    ::

    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.
    #19322
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::
    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!

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: