![unity 3d raycast unity 3d raycast](https://gamedevacademy.org/wp-content/uploads/2017/03/3DSideScrollLineRendererAttachment.png)
We do this to enforce raycasting at an appropriate level: our snail monster takes up almost 2 spaces in the Y axis, so if we raycasting from the Y position of the center of our sprite, we’ll miss the floor and our monster will be stuck going right and trying to find a collision. To Note: if you are paying attention, you will realize we also substracted Vector3(0f, 0.25f, 0f) in the raycast initial position.
#UNITY 3D RAYCAST UPDATE#
To revert the direction in which the monster is going, we are basically doing the same thing as in the Start method: change the value of bIsGoingRight, flip the Sprite, and let our Update loop take care of the rest.
#UNITY 3D RAYCAST CODE#
If we are, it’s to go back the way we came from! The code goes as follows and should be self explanatory: And if there is a collision, check if we are colliding with the ground. The usability for our usecase is obvious: if the monster is going right, check for a collision at a short range. Raycasting if you don’t know already, consists in creating a ray from an initial position, in a certain direction, for a finite or infinite distance and check for collisions. So how do we detect walls? Well, we want the enemy to turn around when it touches a wall, so we’ll be using the simple solution of raycasting. If you put this script on your Monster Game Object, it should move in one direction, great stuff! Now we can have our monster detect walls and turn around.
![unity 3d raycast unity 3d raycast](https://i.ytimg.com/vi/P_nyEPAcWKE/hqdefault.jpg)
Transform.Translate(directionTranslation) Vector3 directionTranslation = (bIsGoingRight) ? transform.right : -transform.right ĭirectionTranslation *= ltaTime * mMovementSpeed if the ennemy is going right, get the vector pointing to its right _mSpriteRenderer = gameObject.GetComponent()
![unity 3d raycast unity 3d raycast](https://gamedevbeginner.com/wp-content/uploads/global-trigger-control.png)
Start is called before the first frame update Public class PatrollingWallToWall : MonoBehaviour We’ll put the logic of detecting walls and turning around in the CheckForWalls function, which we’ll explain in a bit, but for now our class looks like this: The result will be used as an input of the transform.Translate method to move our gameobject according to its movement speed and current direction. So, we’ll use the bIsGoingRight boolean to get the direction our ennemy is going, multiply it by the time elapsed and the monster’s movement speed. What’s more, since our enemies are sprites being displayed, we’ll need to have access to the SpriteRenderer component to flip the sprite when it turns around.įinally, we’ll need our enemy to move. As they are public, we can set their values from the inspector, but we’ll also set some default values. Let’s create a new file for our script: “PatrollingWallToWall.cs”, and set some public properties for the movement speed, and whether the monster is going left or right. Our Proud Snail Guard! Going from Right to LeftĪs this is part of our serie on making a platformer, grounded enemies can only go left or right. While simple, this is widely used in platformers and other kind of games and the logic can be used in many situations. In this article, we’ll be creating an enemy for our 2D platformer in Unity, and implementing a simple behaviour: patrolling from wall to wall. This article is a part of our 2D Platformer Tutorial Serie.
#UNITY 3D RAYCAST HOW TO#
Learn how to use Waypoints instead of raycasting for a more versatile patrolling system in this article: How to make a Patrolling System for Monsters and NPCs in Unity 3D.