Sword Rotation/Sword Position is WAY off. The right side works fine. Help?

So, I’m not sure what’s going on because I followed all the steps to a T.

The one thing I did was import a custom sprite for the sword, but I kept the dimensions and everything else the exact same, just made it more of my own.

My code in this lecture looks the exact same as the lecturer. However, when I turn to the left to just move the mouse and walk around, my sword follows way behind the player. I don’t understand quite how to fix it yet.

Screenshots:

Looking to the right/cursor X is bigger than player X:

Looking to the left/cursor on left:


:

You’ve cut off the inspector values so it’s hard to see what we need to see. Can you show the transform in the inspector (for both directions) for the sword as well as the active weapon, and just to be safe, paste the code for Sword.cs as well. You can look a this post to format the code

Thanks so much for answering so quickly!

I’ll take another screenshot and share it below, please let me know if you need any other info:

As for the code, I have this in the sword.cs:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public class Sword : MonoBehaviour
{

    private PlayerControls playerControls;
    private Animator animator;
    private PlayerController playerController;
    private ActiveWeapon activeWeapon;

    private void Awake()
    {
        playerController = GetComponentInParent<PlayerController>();
        activeWeapon = GetComponentInParent<ActiveWeapon>();
        animator = GetComponent<Animator>();
        playerControls = new PlayerControls();
    }

    private void OnEnable()
    {
        playerControls.Enable();
    }


    // Start is called before the first frame update
    void Start()
    {
        playerControls.Combat.Attack.started += _ => Attack(); 
    }

   private void Update()
    {
        MouseFollowWithOffset();
    }

    private void Attack()
    {
        animator.SetTrigger("Attack");
    }


    private void MouseFollowWithOffset()
    {
        Vector3 mousePos = Input.mousePosition;
        Vector3 playerScreenPoint = Camera.main.WorldToScreenPoint(playerController.transform.position);    

        float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;

        if (mousePos.x < playerScreenPoint.x)
        {
            activeWeapon.transform.rotation = Quaternion.Euler(0, -180, angle);
        } else
        {
            activeWeapon.transform.rotation = Quaternion.Euler(0, 0, angle);
        }
    }




}

Here is the activeWeapon transform as well (which, now that I’m pasting this code, definitely looks not ideal…) :

Stephen’s Active Weapon is sitting at (0,0,0). The sword will be rotating around this point, so if it’s off, it will not rotate the way you’d expect. Reset Active Weapon to (0,0,0) and then adjust the Sword according to the lecture again. It may solve the problem

2 Likes

Privacy & Terms