I have a game that uses a magnetism powerup. Whenever you collide with the Powerup gameobject it enables a Magnetism script placed on the Player GameObject. The script then attracts coins to the player. The magnetism works flawlessly, but for some reason, the coins sometimes speed up and zip by the player faster than every other gameobject. This SHOULD be impossible because I am using a static variable to dictate every gameobjects speed.
Here is the code for the magnet:
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 Transform myTransform;
void Start()
{
myTransform = this.gameObject.transform;
}
void Update ()
{
Collider[] colliders;
Rigidbody rigidbody;
colliders = Physics.OverlapSphere (myTransform.position + m_Position, m_Radius, m_MagneticLayers);
foreach (Collider collider in colliders)
{
if(collider.transform.tag == "coin"){
Currency cScript = collider.GetComponent<Currency>();
if(cScript.canAttract){
cScript.attraction = true;
rigidbody = (Rigidbody) collider.gameObject.GetComponent (typeof (Rigidbody));
if (rigidbody == null)
{
break;
}
/*rigidbody.transform.LookAt(myTransform.position);
rigidbody.transform.Translate(Vector3.forward * BaseClass.speed * Time.deltaTime, Space.Self);*/
Vector3 direction = (this.transform.position - rigidbody.transform.position).normalized;
rigidbody.MovePosition(rigidbody.transform.position + direction * BaseClass.speed * Time.deltaTime);
}
}
}
}
void OnDrawGizmosSelected ()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere (myTransform.position, m_Radius);
}
}
Here is the script I am using for the coins:
using UnityEngine;
using System.Collections;
public class Currency : PowerUp {
// Use this for initialization
public bool attraction=false;
public bool canAttract=false;
public float nDifferenceValue=3;
public int nValue=5;
public static int nMultiplier=1;
public Vector3 startRotation;
//public bool canSpawn=true;
void Start () {
//startPower();
//Debug.Log("Rotation: " + transform.rotation.eulerAngles.ToString());
SpawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint");
startRotation = transform.rotation.eulerAngles;
}
protected override void Update(){
if(speed < 0)
{
speed = 0;
}
//this.gameObject.transform.Rotate(new Vector3(0, -5, 0), Space.Self);
//Debug.DrawLine(this.transform.position, playerobj.transform.position, Color.red);
if (attraction==false){
transform.Translate(Vector3.forward* Time.deltaTime * speed,Space.Self);
//rigidbody.AddForce(Vector3.back * Time.deltaTime * speed, ForceMode.Acceleration);
}
// if (attraction==true canAttract==true){
// /*float difference =Vector3.Distance(transform.position,playerobj.transform.position);
// //Debug.Log(this.transform.position+" "+playerobj.transform.position);
// if (difference<nDifferenceValue){
// //Debug.Log("IN range.");
// Debug.DrawLine(this.transform.position, playerobj.transform.position);
// //transform.Translate(Vector3.back* Time.deltaTime * speed,Space.World);
// //transform.position=Vector3.MoveTowards(this.transform.parent.position, playerobj.transform.position, 2*Time.deltaTime);
// //transform.Translate(Vector3.MoveTowards(this.transform.parent.position, playerobj.transform.position, 2* Time.deltaTime),Space.World);
// //transform.position = (playerobj.transform.position - transform.position ).normalized * 2 * Time.deltaTime;
// this.transform.LookAt(playerobj.transform.position);
// this.transform.Translate(Vector3.forward * 2 * Time.deltaTime);*/
// }
// else {
// //transform.Translate(Vector3.back* Time.deltaTime * speed,Space.World);
// //rigidbody.AddForce(Vector3.back * Time.deltaTime * speed,ForceMode.Acceleration);
// }
//}
}
/*protected override void OnBecameInvisible(){
FindRandomSpawn();
canAttract=false;
}*/
void OnBecameVisible(){
canAttract=true;
}
void OnCollisionEnter(Collision otherObj)
{
//Debug.Log("Hit Something!");
if(otherObj.transform.tag == "Player")
{
// Debug.Log("Hit Player!");
otherObj.transform.GetComponent<Player>().AddScore(nValue*nMultiplier);
StartCoroutine(Die ());
//Destroy(this.gameObject);
}
}
void StartScene ()
{
CameraFade.StartAlphaFade (Color.black, false, 1f, .1f);
}
// public void FindRandomSpawn()
// {
// if (canSpawn==true){
// //Debug.Log("In Random Spawn method");
// GameObject GO = SpawnPoints[Random.Range(0, SpawnPoints.Length)];
// CheckState(GO);
// }
// }
// public void CheckState(GameObject GOtoTest)
// {
// if(GOtoTest.GetComponent<SpawnPoint>().CheckState() == false)
// {
// StartCoroutine(WaitForSpawn());
// }
// else
// {
// ReSpawn(GOtoTest);
// }
// }
//
// public void ReSpawn(GameObject SpawnPoint)
// {
// SpawnPoint.GetComponent<SpawnPoint>().ChangeState("Spawning");
// this.gameObject.transform.position = new Vector3(SpawnPoint.transform.position.x, Random.Range(SpawnPoint.transform.position.y, 0.7f), SpawnPoint.transform.position.z);
// this.gameObject.transform.rotation = Quaternion.Euler(startRotation);
// SpawnPoint.GetComponent<SpawnPoint>().ChangeState("Cooldown");
// }
//
// IEnumerator WaitForSpawn()
// {
// yield return new WaitForSeconds(2.0f);
// FindRandomSpawn();
// }
IEnumerator Die()
{
transform.collider.enabled = false;
transform.renderer.enabled = false;
transform.position=new Vector3(0,0,-15);
yield return new WaitForSeconds(2.0f);
transform.collider.enabled = true;
transform.renderer.enabled = true;
FindRandomSpawn();
}
}
Has anyone else ever experienced this issue? If so, how did you resolve it?
Please, I have tried everything I can think of.