Chiapas Embroidered Blouse

Traditional hand-made embroidered blouse from Aguacatenango, Chiapas. An extremely versatile blouse that can be styled in many different ways. It can be dressed up or worn casually. The choice is…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Creating Bullet Holes via Raycasting

Good morning developers! Now that we’ve got some raycasting under our belts how about we turn those prefabbed red balls into something we’d actually use in our careers? We’re going to go the way of FPS games and add bullet holes to our walls when shot! Let’s make a basic scene with a player character and some walls to shoot.

Since we’re using FPS characteristics we’re going to add a crosshair to our game. We’ll add an image to a canvas and place it in the center of our screen.

Something like this would be perfect!

Since this is on our canvas you can do lots of neat things, like have the image stay in the middle of the screen, even if your character is changing views and moving around.

Let’s make a new Script! We’re going to call this PlayerShoot. Make sure to attach it to the camera your player will be using for this example.

Don’t forget to reference the new input system at the top! The first order of business will be to connect a bullet hole image to the script.

Then just add it by the inspector.

Great! Now let’s go to void update and this time we’re going to do something different for casting the ray. Instead of casting the ray at the mouse position, we’re going to try and cast it from the center of the players screen since that is where we placed our cross hair. The screen can be thought of as the view port. Check the script below and you’ll see that we use that information.

Once the ray is cast we’ll use our hitInfo to collect information and instantiate bullet holes!

Start your program and start clicking around. You’ll know you’re instantiating bullet holes because your inspect will start to fill up with clones, but…

Where are they?

Something interesting about Quaternion.Identity in this case: it actually makes the bullet holes face away from the player on the other side of the wall. We’ll have to take another approach.

Instead of Quaternion.Identity we’re going to use Quaternion.LookRotation(hitInfo.normal) . This is something new, as we’ve always used Identity in the past. You’ll have to keep your eyes out for things such as this that don’t play by normal rules. You’ll also notice that some of you bullets have meshing issues with the wall. There will be things you can do to fix that in the future, but that’s a bit out of scope for now and we may come back to that later!

Hit play and try again.

Perfect! You can see that bullets are being placed from the center of the screen almost as if the crosshair was aiming your shots. We may take efforts in the future to minimalize the meshing issues. But for now the bullet holes face the player and that’s the key point we wanted to reach. I hope you’re starting to get raycasting and I’ll see you again soon!

Add a comment

Related posts:

Lawsuit Filed Against Moneygram For False Information About Partnership With Ripple

Investors have filed a class-action lawsuit against money transfer service MoneyGram International, accusing it of making false claims about the XRP cryptocurrency and its partnership with Ripple…

Why Every Traveling Healthcare Pro Should Have LinkedIn

We all love social media. It helps us stay up-to-date and connected even if you’re a traveling healthcare pro and happen to spend every 12 weeks in a new place. As you move from one assignment to…

Be Your Own Special Unicorn

Everything is rolling smoothly in my creative life, and then something happens. Someone else does something that triggers a lot of negative emotions to surface from inside me. That person is their…