Need a little more help with rotating sword around player (Unity 2D)

Game Development Asked by Gus K on December 23, 2020

this question is somewhat similar to my last question (found here). This time, I have upgraded to the new Input Manager and updated the script that uses the controller, but now I’m having trouble with having the sword rotate around the player and towards the mouse. The image below might help visualize this better.
Crudely drawn picture of a sword rotating around a player towards mouse
Here’s my current code:

public void Update()
    var gamepad = Gamepad.current;
    var mouse = Mouse.current;
    if (controllerControl)
        x = gamepad.rightStick.ReadValue().x;
        y = gamepad.rightStick.ReadValue().y;
        x = mouse.position.ReadValue().x; //Here's where I think the problem is
        y = mouse.position.ReadValue().y;

    //convert the input into an angle in radians, and convert that into degrees
    float rads = Mathf.Atan2(y, x);
    float degrees = rads * Mathf.Rad2Deg;

    //use trig to position sword
    sword.localPosition = new Vector3(Mathf.Cos(rads) * distance, Mathf.Sin(rads) * distance, 0);
    //rotate to face away from center. note that the angle atan gives us is
    //oriented differently than unity's rotation, so we have to reverse it 
    //and add 90.
    sword.localEulerAngles = new Vector3(0, 0, degrees - 90);

The boolean "controllerControl" will change based on which input device the player chooses. The sword is a child of a box object. If someone could point me in the right direction, that would be much appreciated.

One Answer

I found the answer by myself, but if this is not best practice then please let me know.

var gamepad = Gamepad.current;
    var mouse = Mouse.current;
    if (controllerControl)
        x = gamepad.rightStick.ReadValue().x;
        y = gamepad.rightStick.ReadValue().y;
        Vector3 v2 = cam.ScreenToWorldPoint(new Vector3(mouse.position.ReadValue().x, mouse.position.ReadValue().y, cam.nearClipPlane));
        x = v2.x - box.transform.position.x;
        y = v2.y - box.transform.position.y;

This solution requires a new Camera 'cam' if someone else wants to use this.

Correct answer by Gus K on December 23, 2020

Add your own answers!

Related Questions

How to move a character from 1 to 3 squares on a grid?

0  Asked on January 23, 2021 by shunkashuu


Unity 2017.3.of3 Raycast2D randomly misses objects

1  Asked on January 15, 2021 by hesitatetowonder


How to fix the vibration while colliding?

1  Asked on January 13, 2021


Render order in 3/4 view game

0  Asked on January 10, 2021 by chuan-li


How can I control ads frequency?

1  Asked on January 10, 2021 by ruslan-plastun


Attach movement to each list object

1  Asked on January 9, 2021 by mavish


How to make a map surface object

1  Asked on January 8, 2021 by anonymous-entity


Coding Spelunky Edge Roll Mechanic

0  Asked on January 5, 2021 by austin-weaver


Creating a lookup table of datamaps in Harlowe/Twine

0  Asked on January 4, 2021 by the_e


Unity 4 – Some Rigidbodies Won’t Fall Asleep

5  Asked on January 3, 2021 by ryan-berserkguard-nrby


Unity PackageCache keeps having errors

0  Asked on December 31, 2020 by christopher-perry


Phaser Tiled map importing

1  Asked on December 30, 2020 by kabuto178


Change the colour of all particles on screen

1  Asked on December 27, 2020 by super-potato


How can you make a jump to a point using the Character Controller?

0  Asked on December 25, 2020 by aimon-z


Trouble destroying an SDL2 texture from another class

1  Asked on December 21, 2020 by gamer1


How to hide objects behind an invisible plane?

1  Asked on December 18, 2020 by hatinacat2000


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir