- Home /
Can anyone tell me what I'm doing wrong here?
Hi all, I'm trying to create a planetary body type script to have objects fall in and out of orbit based on their original velocity multiplied by the force of gravity emitted by the planet in the direction of the planet.
I've tried what I thought would have worked, but for some reason absolutely nothing is happening to my objects (the script is attached to them).
They are prefabs that are instantiated at random locations but when they collide with the gravity trigger I need them to have force added to them in the direction of the planet.
Here is what I've got so-far:
public float gravityAmount = 100;
float moveSpeed;
Rigidbody2D rb;
public GameObject ball;
bool insideGravity;
void Awake()
{
ball = GameObject.FindGameObjectWithTag("Damager");
}
void Start()
{
insideGravity = false;
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
ball = GameObject.FindGameObjectWithTag("Damager");
if (insideGravity)
{
//This line is the trouble I'm pretty sure.
rb.AddForce((ball.transform.position - transform.position) * gravityAmount * Time.smoothDeltaTime);
}
Vector2 movement = new Vector2(xDirection, yDirection).normalized;
rb.velocity = movement * moveSpeed;
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Magnet")
{
insideGravity = true;
}
}
}
I also thought it could have been using both AddForce and velocity at the same time so I tried using this line instead:
rb.velocity = ball.transform.position - transform.position * gravityAmount * Time.smoothDeltaTime;
But still no change. I'd really appreciate any help you can give me, thanks!
Answer by DiegoSLTS · May 26, 2016 at 01:54 AM
There are a few things that could be wrong, even outside that code. I know some stuff look like too basic, but check anyway:
Make sure the script is enabled when the prefab enters the magnet area
Make sure that both the object with the script and the planet have colliders enabled
Make sure the planet's collider is marked as trigger
At least one of the objects must also have a RigidBody2D (it looks like you have that covered)
Check the layers of the planets and the prefab and make sure their collision is checked in the collision matrix: http://docs.unity3d.com/Manual/LayerBasedCollision.html
Make sure that OnTriggerEnter2D is being called, and, more important, that the line "insideGravity = true;" is being executed. Just write something to the console before that line
Make sure xDirection and yDirection (where are those defined??) are not 0
Make sure moveSpeed is not 0
Make sure there are no errors or warnings in the console
Make sure the Damager and Magnet tags are correctly set in the objects
Also, doing physic stuff in the Update function is not recomended, even if you use smoothDeltaTime. You should change physic values in FixedUpdate if you want to get consistent results independant of the FPS of your game.
Thanks for that, I appreciate the feedback, I actually did write a Debug.Log earlier and tried it out to make sure, turns out yes it was being set to true correctly.
Now I've tried moving my object using AddForce even when not inside the gravity field, it gives a more realistic result (before it just locked there after changing the value of the gravityAmount).
But the issue is that it isn't moving at a constant speed anymore, it starts of at nearly 0 and continues to apply the force every frame.
The other issue is that I had it where it couldn't leave certain X and Y bounds (screen) when it was being moved with Rigidbody2D.velocity. Now it just flys past it but slowly slows down to try to get back (due to already having force applied that it's got to fight past.
Here's where I'm at now:
void FixedUpdate()
{
if (insideGravity)
{
Debug.Log("Is About To Add Force");
rb.AddForce((GameObject.Find("Ball").transform.position - transform.position) * 4000f * Time.deltaTime);
}
Vector2 movement = new Vector2(xDirection, yDirection).normalized;
rb.AddForce(movement * moveSpeed);
if (rb.position.y >= Y_$$anonymous$$ax)
{
yDirection = -1;
}
if (rb.position.x >= X_$$anonymous$$ax)
{
xDirection = -1;
}
if (rb.position.y <= Y_$$anonymous$$in)
{
yDirection = 1;
}
if (rb.position.x <= X_$$anonymous$$in)
{
xDirection = 1;
}
}
Thanks for the FixedUpdate advice, I've implemented it. Any ideas on what to do next?