How to add Rigidbody Required Component to Enemy Follow Function?

I have a problem where I have an enemy that transform moves towards a player, a ball object.

but when the enemy ball moves it floats in a static state towards the player it does’nt roll along the ground like proper Rigidbody movement which is what I want.

here is the script for EnemyMovement1.cs that is attached to the enemy ball object.

using UnityEngine;
using System.Collections;

public class EnemyMovement1 : MonoBehaviour {


    #region Variables
    public Transform target;
    public float speed = 1f;

    public GameObject EnemyExplosionSpawner;
    public GameObject EnemyExplosionClone;

    private ScoreManager EnemyDeathScore;

    #endregion

    #region Basic Functions

    void Start()
    {
        EnemyDeathScore = GameObject.FindWithTag("ScoreManager").GetComponent<ScoreManager>();
    }

    void Update()
    {
        float step = speed * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position, target.position, step);
    }
    #endregion

    #region Collision Function
    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "Bullet")
        {
            GameObject Clone_Enemy_Explode_Handler;
            Clone_Enemy_Explode_Handler = Instantiate(EnemyExplosionClone, EnemyExplosionSpawner.transform.position, EnemyExplosionSpawner.transform.rotation) as GameObject;
            Destroy(Clone_Enemy_Explode_Handler, 5f);
            EnemyDeathScore.EnemyDeathScore();
            Destroy(this.gameObject);
        }
        if (other.gameObject.tag == "Death")
        {
            GameObject Clone_Enemy_Explode_Handler;
            Clone_Enemy_Explode_Handler = Instantiate(EnemyExplosionClone, EnemyExplosionSpawner.transform.position, EnemyExplosionSpawner.transform.rotation) as GameObject;
            Destroy(Clone_Enemy_Explode_Handler, 5f);
            Destroy(this.gameObject);
        }
    }
    #endregion
}

I would like to know, is there a way to get the transform.position = Vector3.MoveTowards(transform.position, target.position, step); function working with Rigidbody movement?

Thanks.

When you update the transfrom.position you are not using the physics system : Your ball doesn’t roll. You need tu use Rigidbody.AddForce for using the physics

ok but I am unable to add this to line 28.

transform.position = Vector3.MoveTowards(transform.position, target.position, step);

what is the correct syntax for this?

thanks

Something like that :

using UnityEngine;
using System.Collections;
public class EnemyMovement1 : MonoBehaviour {
    #region Variables
    public Transform target;
    public float speed = 1f;
    public GameObject EnemyExplosionSpawner;
    public GameObject EnemyExplosionClone;
    private ScoreManager EnemyDeathScore;
    private RigidBody rb;
    #endregion
    #region Basic Functions
    void Start()
    {
        EnemyDeathScore = GameObject.FindWithTag("ScoreManager").GetComponent<ScoreManager>();
        rb = this.GetComponent<RigidBody>();
    }
    void Update()
    {
        float step = speed * Time.deltaTime;
       
        Vector3 forceVector = target.position.Normalize ():
       
        forceVector = forceVector*step*Time.DeltaTime;
       
        rb.AddForce(forceVector, ForceMode.Impulse);
        //you can use different "forceMode"
        //https://docs.unity3d.com/ScriptReference/ForceMode.html
    }
    #endregion
    #region Collision Function
    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "Bullet")
        {
            GameObject Clone_Enemy_Explode_Handler;
            Clone_Enemy_Explode_Handler = Instantiate(EnemyExplosionClone, EnemyExplosionSpawner.transform.position, EnemyExplosionSpawner.transform.rotation) as GameObject;
            Destroy(Clone_Enemy_Explode_Handler, 5f);
            EnemyDeathScore.EnemyDeathScore();
            Destroy(this.gameObject);
        }
        if (other.gameObject.tag == "Death")
        {
            GameObject Clone_Enemy_Explode_Handler;
            Clone_Enemy_Explode_Handler = Instantiate(EnemyExplosionClone, EnemyExplosionSpawner.transform.position, EnemyExplosionSpawner.transform.rotation) as GameObject;
            Destroy(Clone_Enemy_Explode_Handler, 5f);
            Destroy(this.gameObject);
        }
    }
    #endregion
}

FixedUpdate for playing with physics, not Update

1 Like

I am trying this code.

    void FixedUpdate()
    {
        float step = speed * Time.deltaTime;

        Vector3 forceVector = target.position.Normalize();
      
        forceVector = forceVector * step * Time.deltaTime;

        rb.AddForce(forceVector, ForceMode.Impulse);
    }

but I get a red line under target.position.Normalize();

Cannot implicitly convert type ‘void’ to ‘UnityEngine.Vector3’

using UnityEngine;
using System.Collections;

public class EnemyMovement1 : MonoBehaviour {
    #region Variables
    public Transform target;
    public float speed = 1f;
    public GameObject EnemyExplosionSpawner;
    public GameObject EnemyExplosionClone;
    private ScoreManager EnemyDeathScore;
    private RigidBody rb;
    #endregion
    #region Basic Functions
    void Start()
    {
        EnemyDeathScore = GameObject.FindWithTag("ScoreManager").GetComponent<ScoreManager>();
        rb = this.GetComponent<RigidBody>();
    }
    void FixedUpdate()
    {
        float step = speed * Time.deltaTime;
     
        Vector3 forceVector = target.position;
        forceVector.Normalize();
        forceVector = forceVector*step;
     
        rb.AddForce(forceVector, ForceMode.Impulse);
        //you can use different "forceMode"
        //https://docs.unity3d.com/ScriptReference/ForceMode.html
    }
    #endregion
    #region Collision Function
    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "Bullet")
        {
            GameObject Clone_Enemy_Explode_Handler;
            Clone_Enemy_Explode_Handler = Instantiate(EnemyExplosionClone, EnemyExplosionSpawner.transform.position, EnemyExplosionSpawner.transform.rotation) as GameObject;
            Destroy(Clone_Enemy_Explode_Handler, 5f);
            EnemyDeathScore.EnemyDeathScore();
            Destroy(this.gameObject);
        }
        if (other.gameObject.tag == "Death")
        {
            GameObject Clone_Enemy_Explode_Handler;
            Clone_Enemy_Explode_Handler = Instantiate(EnemyExplosionClone, EnemyExplosionSpawner.transform.position, EnemyExplosionSpawner.transform.rotation) as GameObject;
            Destroy(Clone_Enemy_Explode_Handler, 5f);
            Destroy(this.gameObject);
        }
    }
    #endregion
}

this has worked a charm thank you so much :slight_smile:

No actually there seems to be another problem now, now the balls fly off into random directions from the spawner and only some seem to manage to make it to the target location.

how can I get them to roll to the target every time like before? I have the target set in the inspector

thanks

you can check this, i have surely missed something in my code

rb.velocity = (targetPos - transform.position).normalized * speed;

I tried

rb.velocity = (target.position - transform.position).normalized * speed;

now the ball tracks back to the target, bit RigidBody movement is lost and the ball isn’t rolling again.

well try this :

rb.AddForce((target.position- transform.position).normalized*speed,ForceMode.Impulse);
should be fine !

1 Like

ok this looks perfect thanks for your help