Changing the placement of circular motion in 2D?
Hello :D
I've just started in unity a couple weeks back. I am attempting to make a certain circular motion happen when it's in a certain position. However because of the codes i used to create the circular motion, it goes back to the default position of 0, 0 for x and y.
float timeCounter=0;
float speed;
float width;
float height;
void Start () {
speed = 2;
width = 0.3f;
height = 0.3f;
}
void Update () {
timeCounter += Time.deltaTime * speed;
float x = Mathf.Cos (timeCounter) * width;
float y = Mathf.Sin (timeCounter) * height;
transform.position = new Vector2 (x, y);
}
how do i place the circular motion in another position than 0, 0?
Comment
Answer by Nehrk · Mar 14, 2017 at 07:36 AM
Hi. Try to use "%gameObject.transform.position" instead of "transform.position".
#pragma warning disable 649
[SerializeField]
private GameObject _targetGameObject;
[SerializeField]
private Vector2 _offSetPosition = Vector2.zero;
#pragma warning restore 649
float _timeCounter;
float _speed;
float _width;
float _height;
private void Start()
{
_speed = 2;
_width = 0.3f;
_height = 0.3f;
}
private void Update()
{
_timeCounter += Time.deltaTime * _speed;
float x = Mathf.Cos(_timeCounter) * _width;
float y = Mathf.Sin(_timeCounter) * _height;
_targetGameObject.transform.position = new Vector2(x, y) + _offSetPosition;
}