Okay so I am still at it trying to learn how to make a 2D platformer. I am using a simple code to check if the player character is on the ground to know if a jump should take place.
private float groundRadius;
[SerializeField]
private Transform groundPoints;
[SerializeField]
private LayerMask whatIsGround;
[SerializeField]
private float groundRadius;
private bool isGrounded;
private void Update()
{
isGrounded = Physics2D.OverlapCircle(groundPoints.position, groundRadius, whatIsGround);
}
Now in this code I am using a single empty object parented to the player’s feet to know if it is touching the ground. The problem is that the empty object is in the middle of the players feet. So if they stand on a ledge so only the tips of the toes are touching the ground isGrounded won’t be true and the character can’t jump.
What I want to do is turn the empty into an array of three objects parented on the middle and ends of the character’s feet. How ever as it is my code of
isGrounded = Physics2D.OverlapCircle(groundPoints.position, groundRadius, whatIsGround);
Isn’t compatible with an array of simply changing private Transform groundPoints;
into private Transform[] groundPoints;
When I try it I get an error of “Transform[ ]’ does not contain a definition for ‘position’ and no accessible extension method ‘position’ accepting a first argument of type ‘Transform[ ]’ could be found (are you missing a using directive or an assembly reference?)”
How do I adjust the “isGrounded =” portion of the update code to accept the array? Or what function or method of code do I need to use?
Take a look at the Physics2D.OverlapCircle docs, it does not return a bool (yes/no) answer, it returns the specific Collider2D that your circle overlaps or NULL. There’s also an overload that returns ALL Colliders it overlaps too. It doesn’t give you “positions” they overlap and it certainly doesn’t return Transforms which are a specific component on a specific GameObject.
Maybe you’re thinking of RaycastHit2D (not sure) but that comes from a different physics query.
I’m not sure what you mean by “isn’t compatible” beyond just that it doesn’t make sense and the compiler doesn’t understand. These questions seem to be because of your lack of knowledge of how C# works and basic elements such as how types, arrays (etc) work which isn’t any problem at all but this forum isn’t for teaching you that; there’s plenty of tutorials on Unity Learn and YouTube for that. The tutorial you’re following is to be followed to the letter, not to be used to modify when you don’t understand the language. Again, I would recommend some of the beginner scripting tutorials.
I’ll add that there’s a much easier way to detect if you’re grounded however you’ll need to brush up on your C# skills first so I’d definatley suggest doing that.
Anyway, the physics engine allows you to simply detect if you’re “touching” something, you can even filter it by layer(s) and the direction you’re touching. It’s demonstrated here:
https://www.youtube.com/watch?v=ZgsrAHGU-mA
In the end, the detection above comes down to a single line: https://github.com/Unity-Technologies/PhysicsExamples2D/blob/f6f4d8760122e13be8de8b5e3669435787adaac4/Assets/Scripts/SceneSpecific/Miscellaneous/SimpleGroundedController.cs#L17