First off I’m a beginner and this is my first game so please forget my noobness.
So I created a prefab that has a public variable FauxGravityAttractor where I am attaching a GameObject “Gravity” so they will interact with each other. The idea is I need the GameObect “Gravity” to attract (be the center of gravity) to my prefabs.
The original copy of the Prefab is fine, but when I drag new instances of the prefab into my scene, these loose the attractor GameObject “gravity”
[35653-screen+shot+2014-11-22+at+1.24.56+pm.png|35653]
So the Prefab is not holding the "gravity GameObject. Here’s the code:
using UnityEngine;
using System.Collections;
public class FauxGravityBody : MonoBehaviour {
public FauxGravityAttractor attractor;
private Transform myTransform;
// Use this for initialization
void Start () {
rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
rigidbody.useGravity = false;
myTransform = transform;
}
// Update is called once per frame
void Update () {
attractor.Attract(myTransform);
}
}
Not sure if this is relevant, but the GameObject “Gravity” which I am trying to attach to the prefab has this code:
using UnityEngine;
using System.Collections;
public class FauxGravityAttractor : MonoBehaviour {
public float gravity = -10;
public void Attract(Transform body) {
Vector3 gravityUp =(body.position - transform.position).normalized;
Vector3 bodyUp = body.up;
body.rigidbody.AddForce (gravityUp * gravity);
Quaternion targetRotation = Quaternion.FromToRotation (bodyUp, gravityUp) * body.rotation;
body.rotation = Quaternion.Slerp (body.rotation, targetRotation, 50 * Time.deltaTime);
}
}
anyways I found some of these resources that have a similar issue, but I don’t really understanding much of it. THanks