Search Unity

Mouse Follow Aim with IK - 2D Bowman

Discussion in '2D' started by Solengol, Nov 26, 2019.

  1. Solengol

    Solengol

    Joined:
    Nov 23, 2019
    Posts:
    1
    Hi All,

    I'm developing a simple 2D bowman game. I'm trying to implement an aiming system whereby the bowman follows the position of the mouse.

    I want to use IK to make the graphics look more realistic, so I have two bones, one for the body and another for the arm and head, these appear to work fine.

    I have an object for the arm bone with the following script

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Aim_Script : MonoBehaviour
    5. {
    6.     void Update()
    7.     {
    8.         var pos = new Vector2(0, Input.mousePosition.y);
    9.         pos = Camera.main.ScreenToWorldPoint(pos);
    10.         transform.position = pos;
    11.     }
    12. }
    I've then added a Fabrik IK solver to my player sprite, set the object as the effector, set the chain length to 2
    (so it only affects the joints on the arm) and generated a target. When I test this, the player sprite spins around and stutters,

    I can fix this by adjusting the weight, but then it doesn't follow the mouse properly. I suspect that the IK solver is struggling to solve with pos, but I've been at this for a few hours now and I can't wrap my head around it :(