Hello! I wanted to create a “Magnetic Man” so I got this Magnet Script here and put it on my player:
using UnityEngine;
using System.Collections;
public class Magnetic : MonoBehaviour {
public LayerMask m_MagneticLayers;
public Vector3 m_Position;
public float m_Radius;
public float m_Force;
void FixedUpdate ()
{
Collider[] colliders;
Rigidbody rigidbody;
colliders = Physics.OverlapSphere (transform.position + m_Position, m_Radius, m_MagneticLayers);
foreach (Collider collider in colliders)
{
rigidbody = (Rigidbody) collider.gameObject.GetComponent (typeof (Rigidbody));
if (rigidbody == null)
{
continue;
}
rigidbody.AddExplosionForce (m_Force * -1, transform.position + m_Position, m_Radius);
}
}
void OnDrawGizmosSelected ()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere (transform.position + m_Position, m_Radius);
}
}
it works pretty fine, my settings are: Magnetic Layer: Default (my magnetic object are on that layer) Position: 0 - 0,95 - 0,19 (so that the magnetic field is slightly infront of the character) Radius:1 Force:30 (also tried it up to 800)
but the magnetic objects that got picked up, are not rotating with the rotation of the player. so when i walk around, it always stays on the same world position and i want to have it always in front of the characters face. something like a gravity gun
so i editied the script a little bit to this:
using UnityEngine;
using System.Collections;
public class Magnetic : MonoBehaviour {
public LayerMask m_MagneticLayers;
public Vector3 m_Position;
public float m_Radius;
public float m_Force;
public GameObject m_Position_placeholder;
void FixedUpdate ()
{
Collider[] colliders;
Rigidbody rigidbody;
Vector3 m_position = m_Position_placeholder.transform.position;
colliders = Physics.OverlapSphere (transform.position + m_Position, m_Radius, m_MagneticLayers);
foreach (Collider collider in colliders)
{
rigidbody = (Rigidbody) collider.gameObject.GetComponent (typeof (Rigidbody));
if (rigidbody == null)
{
continue;
}
rigidbody.AddExplosionForce (m_Force * -1, transform.position + m_Position, m_Radius);
}
}
void OnDrawGizmosSelected ()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere (transform.position + m_Position, m_Radius);
}
}
And an empty GameObject as a child of your player and positioned where you want the center of the magnetic field located.
Add a reference to that object to this script: publicGameObject m_Position_placeholder;
but i have so say that its not really working as i expected :-/ you can check it out here:NACHGEWIESENE VOR- UND NACHTEILE VON SEO IM BEREICH DES DIGITALEN MARKETINGS - Pixelpizza - Medien and alles drum herum WASD controls and then try to interact with the pink sphere but somehow its still not rotating with the player. has anybody a suggestion how to achieve this?