- Home /
How to attract multiple objects to a target object? C#,
So im very new to coding and Unity. It's my 5th day on doing programming. I am a 3d artist but now I'm also trying to learn coding as an interest and hobby.
I have made a basic starting game. I have a "Player" which is just running and you have to dodge the obstacles and collect the coins.
Now like how in Subway Surfers when you get a power-up, all coins within a range get attracted towards you and are collected.
I'm trying to do the same, but I do not know how.
Here is what I have typed for the attraction of the coins:
public Transform coins;
public Transform magnet;
public float speed;
void FixedUpdate ()
{
coins.position = Vector3.MoveTowards(coins.position, magnet.position, speed * Time.deltaTime);
}
Magnet is the player .. Coins are the coins ( but the problem is, I am only able to reference just 1 coin. Whereas I have 100+ coins in the game ) because when calling public Transform coins, i can only put in 1 coin as a reference.
Help me out and tell me what to do.
How can I attract multiple coins and only when they get in a specific range, because currently the one coin which is being attracted, it starts getting attracted as soon as the game starts. I want it to only happen when it is at a specific distance from the player.
Also also ... I have tried referencing the whole GameObject (group) of the coins and it behaves total opposite, all the coins run away from the player ..
,
Answer by Vincent1236 · Jul 10, 2018 at 08:22 AM
You will have to get all the coins that are around the player, and run the code you have on each of them. Below I do that using the OverlapSphere method. All your coins will need to have a collider somewhere in the prefab. magnetDistance is essentially the radius of the magnet effect.
public Transform magnet;
public float speed;
public float magnetDistance;
void Update ()
{
// Ideally you want to perhaps put coins on a specific physics layer, and use a layer mask to only check that layer (Better performance)
// Take a look at https://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html
Collider[] hitColliders = Physics.OverlapSphere(magnet.position, magnetDistance);
for (int i = 0; i < hitColliders.length; i++)
{
// do whathever you need here to determine if an object is a coin
// Here I assume that all the coins will be tagged as coin
if (hitColliders[i].tag == "coin")
{
Transform coin = hitColliders[i].transform;
coin.position = Vector3.MoveTowards(coin.position, magnet.position, speed * Time.deltaTime);
}
}
}
Answer by UnitedCoders · Jul 11, 2018 at 09:55 AM
Use this method. Make a big sphere collider around your player, and when you collect power up. then check collision with your collectable item. and OnTriggerEnter(Collider other) { if(other.gameObject.tag=="COLLECTABLE_TAG") { other.SendMessage("CanAttract"); } }enter code here
public Transform target;
bool canAttract=false;
void Update () {
if (canAttract) {
Vector3.MoveTowards (this.transform.position, target.transform.position, 1 * Time.deltaTime);
}
}
void CanAttract()
{
canAttract = true;
}
This answer doesn't make any sense, this is like you're combining a player script, and the coin script. You're looking if a object is entering the player's spherical collision area, yet then you move the player itself towards the player itself, on the 6th line.
Answer by Janibasha · Jul 16, 2018 at 10:27 AM
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CoinsBehaviour : MonoBehaviour { private GameObject player; private float CoinSpeed = 30.0f; private Transform playerTransform;
public float CoinRotateSpeed = 5;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
playerTransform = player.GetComponent<Transform>();
transform.position = new Vector3(transform.position.x, transform.position.y, 0f);
}
void Update()
{
if(PlayerScript.isMagnetPowerup == true)
{
CoinsPowerup();
}
}
void FixedUpdate()
{
transform.Rotate(Vector3.down * CoinRotateSpeed);
}
public void CoinsPowerup()
{
float distance = Vector3.Distance(playerTransform.position, transform.position);
// float distance = playerTransform.position.y - transform.position.y;
float maxDistance = 10.0f;
// float maxDistance = 0.030f;
if (distance < maxDistance)
{
// Debug.Log ("Now near in: "+ Vector3.Distance(playerTransform.position, transform.position));
transform.position = Vector3.MoveTowards(transform.position,playerTransform.position + new Vector3(0,-2,0), CoinSpeed * Time.deltaTime);
}
}