- Home /
How do I get instantiated rigidbodys to move towards a point?
I'm making a basic game that involves generating several spheres around a larger one at random points. I need these spheres to move towards the center of the larger one, but cannot figure out how to add velocity in such a way that it moves the spheres towards the larger sphere. I have also been unable to get more than one sphere to have force applied to it. Code below
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeteorSpawn : MonoBehaviour
{
public float speed = 50f;
public int meteorAmount = 5;
public GameObject meteor;
public GameObject rotator;
Rigidbody rb;
// Start is called before the first frame update
void Start()
{
for (int i = 0; i < meteorAmount; i++)
{
float randomX = Random.Range(0f, 360f);
float randomY = Random.Range(0f, 360f);
float randomZ = Random.Range(0f, 360f);
meteor = Instantiate(meteor, transform.position + Vector3.up * 100, Quaternion.identity);
meteor.transform.parent = rotator.transform;
rotator.transform.Rotate(new Vector3(randomX, randomY, randomZ));
rb = GetComponentInChildren<Rigidbody>();
rb.AddForce(Vector3.up * speed);
}
}
// Update is called once per frame
void Update()
{
}
}
Would transform.LookAt() with the transform of the large sphere and adding a forward velocity do?
Yep that worked perfectly thank you. I wasn't aware there was a transform.Lookat, since I am still new to this, and for whatever reason the velocity need to downwards but it works now thanks.
Answer by fantommemelord2727 · Jul 26, 2020 at 05:54 AM
You could try using the move towards function, You just have to give a transform to move to.
Your answer
Follow this Question
Related Questions
Make a object move forwards 1 Answer
Have player fall down with platform? 0 Answers
Smooth dashing issues 0 Answers
Freeze Position + collision? 0 Answers
When Movement speed Change once it never change again 1 Answer