For my game I’d like to have magic spells that crawl across the surface. Meaning specifically that they can move uphill/downhill and up/down slopes (but nothing too steep such as 60 degree + angles. Can’t have them crawling up against walls or boundaries).
The problem is that the spell needs to be able to go through enemies and certain objects. If I set the collision to “Is Trigger” the spell will fall through the floor.
How can I get it to:
- Move along the surface
- Not fall/move through the surface
I Managed to resolve this issue but I’m not very happy with it. It works however. Let me explain how I achieved this for anyone who needs it for future reference.
I started out by making a cude, adding a ridgidbody, and material set to ice (to that it can slide). On instantiating the floor is automatically located. It’s moved by rigidbody.addforce. Rotation is locked for x y and z (else the visual aspects of the spell will look strange).
To prevent it from flying I’ve made an IsGrounded method that looks if there’s an object between the cubes position and 10 away. If there is, the cube is moved to that position (+ offset) if there isn’t the object is destroyed (prevents falling off map etc).
The above ‘sticks’ the cube to the surface.
For collision I did the following: I created 2 new layers, a spellLayer and a LevelLayer. The levellayer is basically every surface. The spelllayer only collides with the levellayer. (Project settings > Physics). The cube gets the spelllayer.
Next I created an empty gameobject as the child of the cube. Added a boxcollider to this, and added a collision method. (Box collider to istrigger).
Since I can only add a “spell” class to my magic weapons I made both the cube and the child a spell. On start I copy all the required data from the cube to the child.
The method below is on the child and basically checks for many targets have been hit and destroys it if the maxhits is exeeded. The cube is destroyed after a set amount of time (based on the max distance it may travel).
As I said, it works, but it seems a bit sloppy. I will try to improve this if possible. Feel free to leave a comment.
public void HitsCount()
{
if (this.MaxTargets < 0)
{ return; }
Currenthits++;
if (Currenthits >= this.MaxTargets)
{
Destroy(transform.parent.gameObject);
}
}
“Is Trigger” disables collisions, if you want it to not collide with enemies and certain objects try using this: