- Home /
I'm trying to make it so when I left click, my character's sword slash sprite appears towards the direction of the mouse
This is my code so far:
[Command]
void CmdSlash() {
GameObject go = Instantiate (CurrentAttackPrefab);
float speedTurn = 10f;
Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition);
float angle = Mathf.Atan2 (direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis (angle, Vector3.forward);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, speedTurn * Time.deltaTime);
NetworkServer.Spawn (go);
}
And this is in my update
if(Input.GetKeyUp(KeyCode.Mouse0)) {
CmdSlash();
}
I'm sure most of this is wrong, I'm trying to put together code from different tutorials and guides. What ends up happening is with each left mouse click, my character will rotate away in some increment, until he is facing 180 degrees away, and the sword slash sprite just spawns at 0, 0, 0. I don't want my character sprite to rotate, i just want the slash sprite to appear towards the direction of the mouse relative to the character. Later I will implement the sprite's arm moving appropriately relative to the direction of the slash, so for now I just want it to not rotate at all. It's also supposed to be online, but I don't think that changes a whole lot of code. Any help or guidance would be greatly appreciated, thanks.
Ins$$anonymous$$d of Instantiate, attach slash sprite to your pawn, then disable it. When it comes to slash command just enable it again. That way you wouldn't deal with it position because its attached to pawn. Also it is proper way because instantiate is performance poor, and you use it when you need another distinct copy of the object
transform.rotation is bound to your pawn, not the instantiated slash sprite, that's why you pawn rotates ins$$anonymous$$d of slash.
Hope these helps.