1
votes

I'm making a game which involves rotating around a point shooting enemies before they reach said point, in 2D. However, as the enemies are going to be spawned in random places around the game, I need to make them rotate towards the centre to start moving in that direction. Here is the code that doesn't function properly:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    public Rigidbody2D rb;

    //this is the enemy, by the way
    public Transform follow; 

    public void Start()
    {
        transform.LookAt(Vector2.zero);
        rb.velocity = follow.up; //'up' worked in my bullet script for heading in the direction the player was facing
    }
}

Instead of the knob rotating and slowly moving towards the point, the knob weirdly elongates and then begins to move towards the top of the screen, but at a slight angle. It's probably a stupid mistake, as I've only been writing in C# and using Unity for about a week.

However, any help would be greatly appreciated!

2
When you select the enemy sprite/gameobject, with the move gizmo showing, what color arrow is pointing in the direction the enemy is facing? (or away from that direction)Ruzihm

2 Answers

0
votes

Edit: After clarification, this is my answer:

You should use Vector3.MoveTowards.

void Update ()
{
    enemy.transform.position = Vector3.MoveTowards(enemy.transform.position, point.transform.position, speed * Time.deltaTime);
}

I would recommend attaching this to your enemy game object and replacing enemy.transform.position; with gameObject.transform.position;. This way you can make an enemy prefab later on and easily spawn as many as you need.

https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html


Sorry if I don't understand fully, but you want the enemys to rotate and move towards the center of the screen?

If so, you can use transform.Rotate(rotation * Time.deltaTime); to rotate the enemys and transform.Translate(vector * Time.deltaTime);to move them towards a point.

Add the game object you want to move to the method. Eg, if this were going to be attached to your enemies script you would write gameObject.transform.Rotate(rotation * Time.deltaTime);.

If you wanted the enemys to circle around something you could create a 'point' game object then attach the rotate script to it. Then make the enemies children of that object so they rotate with it. Then, put the translate script on the enemys so they move towards the point.

Hope this helps!

https://docs.unity3d.com/ScriptReference/Transform-rotation.html https://docs.unity3d.com/ScriptReference/Transform.Translate.html

0
votes

Try this. Transform.LookAt might not be working for 2d. See this for similar example.

using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    public Rigidbody2D rb;
    public float speed = 1f;
    public Transform follow; //this is the enemy, by the way
    public void Start()
    {
        //depending how your enemy points (is its forward direction x axis or y axis) you should either use transform.right or transform.up
        transform.right = follow.position - transform.position;
        // or use transform.up = follow.position - transform.position;
        rb.velocity = transform.right * speed;  // change transform.right to transform.up if you use transform.up above.
    }
}