Now I was able to figure out how to increase the velocity to another object and actually keep following it, but what I want is to detect the position of the target only once so it is not necessary to collide but they will go close to each other depends on the speed. I used this (it has some rotation code too so don’t mind that:
- Rigidbody2D rb2d;
- publicfloatSpeed;
- publicfloatZigZag;
- privateGameObject targetEN;
- // Use this for initialization
- voidStart()
- {
- rb2d =GetComponent();
- targetEN =GameObject.Find(“EN_TUT_01”);
- }
- // Update is called once per frame
- voidUpdate(){
- var distance =Vector3.Distance(transform.position,targetEN.transform.position);
- Vector2 dir = rb2d.velocity;
- float angle =Mathf.Atan2(dir.y, dir.x)*Mathf.Rad2Deg;
- if(rb2d.velocity.y <=0)
- {
- transform.rotation =Quaternion.Euler(0,0,90);
- }
- else
- {
- transform.rotation =Quaternion.AngleAxis(angle,Vector3.forward);
- }
- if(distance >6)
- {
- rb2d.velocity =newVector2(Random.Range(-ZigZag,ZigZag),Speed);
- }
- else
- {
- rb2d.velocity =(targetEN.transform.position - transform.position).normalized *Speed;
- }