After implementing the different weapon prefabs and bow/mouse aiming, my sword now will only point left and the bow will only move slightly rather than opposing my mouse position if i circle the player.
Character not flipping the sword:
It’s hard to show the mouse cursor/bow issue as screenshots dont capture the cursor. But if its close to the left of the character, the bow is on the left side of the character, if the cursor is far to the left of the character, the bow is on the right of the character.
The bow does aim correctly if the cursor moves around the edge of my screen, but if it’s close to the player it doesnt seem to behave correctly.
Not sure where i would begin to look at code to troubleshoot this. Any help appreciated
i changed the directional facing to be based on character movement rather than the mouse pointer which worked fine - i cant see that this would interfere but maybe you’ll see something different.
With this code, the sword will never do more than face left or right, but never actually change anything with the mouse, including aiming.
I’m not sure, however, why it wouldn’t move left and right as your character moves left or right as mentioned at the top of your initial post (and you seem to be saying it’s working fine now?)
As the course is setup using the New Input System, are you getting any error messages concerning the Input.GetAxisRaw(“Horizontal”)?
Yeah the IF statements are firing fine, no error logs, console messages fire as expected. Obviously the bow will need mouse movement to aim so would assume a method in the bow class would override.
However, had a look at the classes attached to the Active Weapon game object in the hierarchy and Mouse Follow was on there, sure id have put that on to follow the steps in the vid but obviously this conflicts with the directional facing based on move direction rather than mouse position.
Suppose I could do something like add an if statement there for the facemouse method that if activeweapon slot != 0
I should have slept on it before commenting, sometimes all you need is more mental bandwidth to look with fresh eyes
Although im struggling to see how i could check if the weapon is the sword then not to apply the mouse follow - though the bow maybe is being interfered with due to the directional facing and why it isnt moving properly. Is there a way to separate these out? Or would that require a different kind of code game setup? im thinking it would.
for example, it would require each weapon in the hierarchy and as you toggle through the weapons it enables and disables accordingly and changes the sprite, but in each one the hierarchy has different attack and key/mouse tracking scripts attached to it?
The bow class just has an empty public void Attack() method for implementing the IWeapon interface. Other than that it’s empty which i think for this point of the tutorial is what it should be?
I tried reverting to the cursor based positioning for the sword method but it didnt affect the bow not rotating correctly.
Ah fair. MF and Aw attached to active weapon object:
Mouse Follow class below. Not sure why the bow wouldnt rotate correctly, but i get the concept and can live with it for the sake of the tutorial. ill just use the sword.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseFollow : MonoBehaviour
{
private void Update(){
FaceMouse();
}
private void FaceMouse(){
Vector3 mousePosition = Input.mousePosition;
mousePosition = Camera.main.WorldToScreenPoint(mousePosition);
Vector2 direction = transform.position - mousePosition;
transform.right = -direction;
}
}
I think this is it, entirely. WorldToScreenPoint(mousePosition) would convert our world scale (depending on your setup around ±20,±15) to Screen point which will
Always be positive
Will be on a MUCH larger scale, as it represents pixels on your game window, not World Units.
Try changing this to ScreenToWorldPoint, and I think you’ll see improved results.