Najjar
Najjar

Reputation: 9

Unity Transform,position

I am facing an issue as follow:

The circle is located circle location before play the game.

when press play it goes to another place as shown here after press play

here is the code I am using, im pretty sure its a if statement issue:

public class circularmouse : MonoBehaviour {
[SerializeField] float timeCounter = 0;
[SerializeField]bool Direction = false;
[SerializeField] float angularSpeed = 0f;

public Vector3 startPosition;

private void Start()
{
    startPosition = transform.position;
}




void Update()
{
    if (Input.GetKeyDown(KeyCode.LeftArrow))
    {
        angularSpeed = 4f;
        Direction = true;
    }
    if (Input.GetKeyDown(KeyCode.RightArrow))
    {
        Direction = false;
        angularSpeed = 4f;
    }
    if (Direction) //if direction is true
    {
        timeCounter += Time.deltaTime * angularSpeed;
        float x = Mathf.Cos(timeCounter);
        float y = Mathf.Sin(timeCounter);
        transform.position = new Vector3(x, y, 0);
    }
    else
    {
        timeCounter -= Time.deltaTime * angularSpeed;
        float x = Mathf.Cos(timeCounter);
        float y = Mathf.Sin(timeCounter);
        transform.position = new Vector3(x, y, 0);
    }

your help is appreciated

Upvotes: 0

Views: 2343

Answers (1)

ryeMoss
ryeMoss

Reputation: 4343

Your issue is that since Direction is initially false, you are immediately going to this line:

transform.position = new Vector3(x, y, 0);

which is setting your transform to the position (1, 0, 0).

Not entirely sure what you are trying to accomplish, but maybe you instead mean to add the coordinates to the position?

transform.position += new Vector3(x, y, 0);

Upvotes: 2

Related Questions