Reputation: 15
I'm currently creating a 2D top down game with neural network controlled enemies. I am using the rigidbody2d to control their movement but when they bump into eachother they go straight through and don't collide. Please help! the code if below (output[] are the outputs from the nn):
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
outputs = GenerateOutputs();
Debug.Log(outputs[0]+"||"+outputs[1]);
currentRotation = outputs[0] * rotationSpeed;
myRigidbody.MoveRotation(myRigidbody.rotation + currentRotation);
currentSpeed = ((outputs[1] + 1) / 2) * speed;
Vector2 velocity = transform.up * currentSpeed;
myRigidbody.MovePosition(myRigidbody.position + velocity);
}
Here is the Rigidbody2d/CircleColider2D setup:
Here is the layer collision matrix (the enemies are on the 'Predator' layer:
Upvotes: 0
Views: 52
Reputation: 461
You need to make the RigidBody2d have a BodyType of "Dynamic". This will allow the physics to take control.
I'm not sure of your exact control model, however, you may run into an issue with multiple objects using that FixedUpdate code. MoveRotation and MovePosition will affect physics objects and be affected by physics (kind of). However, since they are setting the position and rotation, the objects with that code won't dynamically bounce around. You would want to use AddForce and AddTorque if you want everything bouncing around.
Upvotes: 0