Evaluating collisions in Visual Scripting

I’m following a series of tutorials and have been trying to convert them from C# script into visual scripts.

The tutorials are found at this link.

I’m a novice, and am using Visual Scripting as a way to wrap my mind around c#.

How would I go about turning this set of instructions into a visual script? The parts that are really blowing my mind are bolded and red, but I went ahead and put more than strictly necessary for the sake of context.

No Wall Jumping
Allowing a jump when touching anything means that we could also jump when in the air but touching a wall instead of the ground. If we want to prevent that we have to be able to distinguish between the ground and something else.
It makes sense to define the ground as a mostly horizontal plane. We could check whether what we’re colliding with satisfied this criteria by inspecting the normal vector of the contact points of the collision.

brilliantlongdartfrog

We can get the collision information by adding a Collision parameter to both OnCollisionEnter and OnCollisionStay. Instead of directly setting onGround to truewe’ll forward that responsibility to a new EvaluateCollision method, passing it the data.
void OnCollisionEnter (Collision collision) {
//onGround = true;
EvaluateCollision(collision);
}
void OnCollisionStay (Collision collision) {
//onGround = true;
EvaluateCollision(collision);
}

void EvaluateCollision (Collision collision) {}

The amount of contact points can by found via the contactCount property of Collision. We can use that to loop through all points via the GetContact method, passing it an index. Then we can access the point’s normal property.

void EvaluateCollision (Collision collision) {
for (int i = 0; i < collision.contactCount; i++) {
Vector3 normal = collision.GetContact(i).normal;
}
}
The normal is the direction that the sphere should be pushed, which is directly away from the collision surface. Assuming that it is a plane, the vector matches the plane’s normal vector. If the plane were horizontal then its normal would point straight up, so its Y component should be exactly 1. If this is the case then we’re touching the ground. But let’s be lenient and accept Y components that are 0.9 or greater.

Vector3 normal = collision.GetContact(i).normal;
onGround |= normal.y >= 0.9f;

Any help is appreciated! I’m making loads of progress with learning, but I’m not quite sure what this tutorial is asking me to do in this step.

Okay, some small progress here.

First, the majority of the problems I’m having are due to differences in naming between Visual Scripting and c#. For instance, when c# calls for “collision” from OnCollisionEnter, visual scripting calls this “data.”

Is there perhaps a handy code - to - visual scripting dictionary somewhere out there?

If you use the graph inspector, when you click on a node it shows you all the input/output types. That is your link to what things are. You can refer to the script reference, that’s your best guide.

Unity - Scripting API: Collider.OnCollisionEnter(Collision) (unity3d.com)

Unity - Scripting API: Collider (unity3d.com)

1 Like

Thanks for the help!

For anyone in the future, this is my solution to this particular block of code.

It appears that (as of 2021.3.3f1) Visual Scripting doesn’t currently let you take collision data and turn it into a variable like the instructions tell you to do. In the node for “On Collision Enter/stay/exit”, there is a “data” output that gives you the type “collision”, but I fiddled around a bit and couldn’t seem to get it to work. Not saying it’s broken, just that it is, at the least, not intuitive for me.

Here’s a shot of the solution I worked up. It does essentially the same as the code in the tutorial, but is in visual scripting:

Take this with a grain of salt, of course, because I’ve only been doing anything even remotely related to coding for about a month, and am just self-teaching as a hobby. But at least as far as I can tell, this does work.