Question by
Reetro · Aug 10, 2020 at 07:12 PM ·
2d game2d-platformer2d-gameplay
Make a spear to jab enemies with
So am trying to setup a spear that do a quick jab. In the direction it's facing (currently being rotated by mouse in my most recent setup). Here is a diagram to show what I mean
Current code setup (not great and pretty buggy). Rotating it works fine just moving it between 2 points is what am having problems with.
void Update()
{
RotateSpearWithMouse();
if (pushSpear)
{
transform.position = Vector2.Lerp(transform.position, pushTransform.position, (Mathf.PingPong(Time.time * pushSpeed, 1)));
pushSpear = false;
canRotate = false;
}
}
push spear is being set to true when player clicks the left mouse button. Then push transfrom is a gameobject attached to the spear in the editor. The spear also will rotate towards the mouse. Code to that here
private void RotateSpearWithMouse()
{
Vector3 mousePos = controls.Player.MousePostion.ReadValue<Vector2>();
Vector3 spearPos = Camera.main.WorldToScreenPoint(transform.position);
mousePos.x = mousePos.x - spearPos.x;
mousePos.y = mousePos.y - spearPos.y;
spearAngle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
if (MouseLeftOrRight())
{
transform.rotation = Quaternion.Euler(new Vector3(180f, 0f, -spearAngle));
}
else
{
transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, spearAngle));
}
}
diagram.png
(30.0 kB)
Comment