Hello there unity community. I would like some help as I have been asking a lot of people but didn’t get good answers really they were confused on what I’m trying to do, hopefully someone in here can help me. I’m new to unity and have been working on some fun projects and such. So my current objectives is follows: I want a player to have a radius or some type of range (When i increase the sphere collider player starts to bounce around like a rabbit). Lets say I pick up a cube or any gameObject that is collectable and gives me points, I want that gameObject to be a trigger that will activate some type of black hole or gravity within the range/radius that is around a player, meaning everything will get pushed/pulled/or sucked in to the player and collected (gameObjects). Please help, suggestions on where to look at. Thank you.
If your really new to unity prog, start with something easier I think.
If not, you should be able to attract objects to the player with the rigidbody, by turning of the gravity and creating a new gravity by yourself. To get the direction, you do :
Vector3 dir = player.transform.position - objectToAttract.transform.position;
Then, you should be able to move the object position among this new axis(dir).
To collect the object, just check if the distance between the player and the object is less than x, and if so, you remove the object from the game, and add it to the player inventory or whatever.
ok so my current code is this :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MagneticField : MonoBehaviour
{
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag(“MagneticTrigger”))
{
}
}
}
I’m trying to set it to be on trigger function, and so that the player is the center. Meaning all the gameObjects move to the player.
Make sure you do a couple things here: normalize your direction vector, and make sure you use ForceMode.Accleration.
ForceMode.Accleration?
How do i even attract a gameobject?
so this is my code right now. This is a pick up gameObject that i want to trigger the force
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MagneticTrigger : MonoBehaviour {
public GameObject[ ] pickupObjects;
public int score = -4;
float delay = 0.1f;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag(“Player”))
{
ScoreManager.AddPoints(score);
Destroy(gameObject, delay);
}
}
}
So lets assume that you have already identified the gameobject that is the ‘black hole’. In the code sample you provided I assume that the attractor obj would be:
other.gameObject
So from here you need to get a list of physics objects within a range. For this, I like to use Physics.OverlapSphere. This will return an array of colliders attached to gameobjects within range.
From here, you need to loop through the array (easiest with a foreach loop). Foreach collider objecr in the array you need to calculate force vectors and apply the forces necessary. This would look something like this:
//calc direction vector towards gravity source
Vector3 dir = (other.transform.position - collider.transform.position).normalized;
//add forces
if (other.GetComponenent <Rigidbody>()){
other.transform.GetComponenent <Rigidbody>().AddForce (dir * gravity, ForceMode.Acceleration);
}
I like to use ForceMode.Acceleration for anything involving planetary bodies because this will apply a force per kilogram instead of a force per mass. This is the proper way to do in such a case (a value of 9.81 for gravitg would be Earth gravity). But you dont have to do it this way.