- Home /
2D Realistic Space Collision.
Im making a 2D space sandbox game where you are a astroid/planet and just explore. But the problem I have is that when I hit another object (planet/astroid) they don't "bounce" away or move away from the player. Im using Circle Collider and tried using rigidbody on both the object and the player but nothing works.
I think it might be becasue of the the movment script I use. It based on a concept that if I push down a button it will accelerate and if I let go it will deaccelerate. Here is the code for the movment if someone is wondering. public class TestingMovesKeys : MonoBehaviour {
float ShipCurrentSpeed = 0f;
public float ShipTopSpeed = 40f;
float ShipCurrentSpeedX = 0f;
public float RevShipTopSpeed = -40f;
public float ShipAcceleration = 1f;
public float ShipDeacceleration = 0.5f;
void Update()
{
if (Input.GetButton("W") == false) // If im not holding down "W"
{
if (ShipCurrentSpeed > 0)
{
ShipCurrentSpeed -= ShipDeacceleration;
}
}
else if (Input.GetButton("W") && (ShipCurrentSpeed < ShipTopSpeed)) // If im holding down "W"
{
ShipCurrentSpeed += ShipAcceleration;
}
if (Input.GetButton("S") == false) // If im not holding down "S"
{
if (ShipCurrentSpeed < 0)
{
ShipCurrentSpeed += ShipDeacceleration;
}
}
else if (Input.GetButton("S") && (ShipCurrentSpeed > RevShipTopSpeed)) // If im holding down "S"
{
ShipCurrentSpeed -= ShipAcceleration;
}
// X-axis
if (Input.GetButton("D") == false)
{
if (ShipCurrentSpeedX > 0)
{
ShipCurrentSpeedX -= ShipDeacceleration;
}
}
else if (Input.GetButton("D") && (ShipCurrentSpeedX < ShipTopSpeed))
{
ShipCurrentSpeedX += ShipAcceleration;
}
if (Input.GetButton("A") == false)
{
if (ShipCurrentSpeedX < 0)
{
ShipCurrentSpeedX += ShipDeacceleration;
}
}
else if (Input.GetButton("A") && (ShipCurrentSpeedX > RevShipTopSpeed))
{
ShipCurrentSpeedX -= ShipAcceleration;
}
// X-axis
if (ShipCurrentSpeedX >= 0)
{
transform.Translate(Vector3.right * Time.deltaTime * ShipCurrentSpeedX);
}
if (ShipCurrentSpeedX <= 0)
{
transform.Translate(Vector3.right * Time.deltaTime * ShipCurrentSpeedX);
}
// Y axis
if (ShipCurrentSpeed >= 0)
{
transform.Translate(Vector3.up * Time.deltaTime * ShipCurrentSpeed);
}
if (ShipCurrentSpeed <= 0)
{
transform.Translate(Vector3.up * Time.deltaTime * ShipCurrentSpeed);
}
}
}
Answer by Landern · Dec 14, 2016 at 02:37 PM
You're using Translate which will set the transform and position. If you want to use the built in physics engine along with everything it provides given the material settings you need to start using FixedUpdate and AddForce on the RigidBodies(Rigidbody2d). This will allow you to push the calculations onto the physics system while you maintain the settings for the colliders and things i've already mentioned. You can find plenty of tutorials all over youtube for this.
Your answer
Follow this Question
Related Questions
Gravity Direction Change 2D 1 Answer
2D swinging physics. 0 Answers
New Pointer Effector 2D Help 0 Answers