I want to create a game similar to Loop Mania on the iPhone App Store. For this, I am calling upon Vector2 and using trigonometric functions which move my object in a circle. However, I want the object to translate to the negative values of the coordinates in a straight line when the left button on a mouse is clicked. For example, if my object is at (2, 3) at a certain point in time, I want it to translate in a straight line to (-2, -3). This is my code so far. I appreciate any and all help.
private float x, y, a, b;
private float timeCounter;
private float speed;
private float slope;
private Vector2 targetPosition;
private void Start ()
{
x = 2.95f;
y = 0f;
timeCounter = 0f;
speed = 2.5f;
}
//Updates x and y coordinates of the circle
private void Update()
{
timeCounter += Time.deltaTime * speed;
x = Mathf.Cos(timeCounter) * 2.95f;
y = Mathf.Sin(timeCounter) * 2.95f;
a = -x;
b = -y;
transform.position = new Vector2(x, y);
targetPosition = new Vector2(a, b);
translateAcross();
}
private void translateAcross()
{
if(Input.GetMouseButtonDown(0))
{
transform.position = Vector3.MoveTowards(transform.position, targetPosition, timeCounter);
}
}