# Brackeys “angry birds” modification – how to get “bird” move along a direction w/ mouse?

Updated on March 11, 2019 in
0 on March 11, 2019

edit: going back over a script, I realized I sort of got working what I wanted with the mouse following the Jonah transform. The only problem seems to be if the mouse moves fast enough then Jonah’s position isn’t always accurately updated and large distances are added between the mouse and Jonah’s transform. I’m not sure if there’s a way to fix this or not.

here’s a gif of what’s happening

```
//run from FixedUpdate

//if jonah is clicked on from OnMouseDown()
if (isPressed)
{
//get mousePos
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.z = 0;

//first frame on click, subtract the position
if (!havefirstPrevPos)
{
float xAbs = Mathf.Abs(transform.position.x);
float yAbs = Mathf.Abs(transform.position.y);
prevPos = new Vector3(xAbs, yAbs);
havefirstPrevPos = true;
}

//get abs value to apply
Vector3 mousePosAbs = mousePos;
mousePosAbs.x = Mathf.Abs(mousePos.x);
mousePosAbs.y = Mathf.Abs(mousePos.y);

Vector3 prevPosAbs = prevPos;
prevPosAbs.x = Mathf.Abs(prevPosAbs.x);
prevPosAbs.y = Mathf.Abs(prevPosAbs.y);

Vector3 changeInPos = mousePosAbs - prevPosAbs;

Debug.Log(mousePos + ", " + prevPos + ", " + changeInPos);

//if change is big enough to be recognized
if (Mathf.Abs(changeInPos.magnitude) > 0.001f)
{
//if mouse is moving to the right
if (mousePos.x > prevPos.x)
{
//apply mouse pos change between frames
transform.position += changeInPos.magnitude * new Vector3(go_whale.up.x, go_whale.up.y, 0);
}
//if mouse is moving to the left
else if (mousePos.x < prevPos.x)
{
//apply mouse pos change between frames
transform.position -= changeInPos.magnitude * new Vector3(go_whale.up.x, go_whale.up.y, 0);
}
}

prevPos = mousePos;

```

(I posted this in the Unity scripting forum but only received one response)

I’m working on a small goofy religious game about Jonah and the whale, and am using Brackeys “Angry Birds” tutorial as reference. (great video of course) Following another online tutorial, I got the whale to constantly point in the direction of the mouse (slowed down using Slerp).

When Jonah is clicked and the mouse is held, I want Jonah to only be able to move up and down in the direction the whale is facing (and the whale always points towards the mouse), like Jonah is on a straight track. I don’t know the relevant math well so I’m not sure how to figure this out. I tried getting the distance the mouse moves in each frame and applying it to Jonah in the direction (whale_transform.up), but it doesn’t work accurately; despite using ScreenToWorldPoint, as Jonah moves farther from the whale, the amount of distance applied to Jonah seems to get larger than the mouse movement change each frame.

Any idea what to do? I know of the existence of Sin, Cos, Tan, hypotenuse, y = mx + b  but don’t know if or how to work that into what I need to do. Thanks for any help in advance!

• Liked by