- Home /
Object "crawling" over floor, how?
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
Is there a good reason you're using rigidbody physics to drive your "projectile"? If you want to use that approach, you could feasibly solve the issue by separating your respective objects into different layers. Only let the projectile physically interact with the ground, and have a secondary trigger collider on the projectile which only reacts to things the projectile can affect.
No, I just don't know how else to move something across the floor. Do you have a better suggestion? And how would collision with specific layers work in code? Do you have a reference to unity docs?
Answer by Ricewind1 · Nov 05, 2014 at 05:08 AM
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);
}
}
Answer by Steampunk_Droid · Nov 05, 2014 at 12:58 AM
"Is Trigger" disables collisions, if you want it to not collide with enemies and certain objects try using this: http://docs.unity3d.com/ScriptReference/Physics.IgnoreCollision.html
Is trigger doesn't disable collision. In fact, "is trigger" is default for projectiles hitting rigid-bodies to prevent them from moving (if unwanted). The main problem is getting it to move across the surface while is trigger is enabled.
Your answer
Follow this Question
Related Questions
Can't get gravity to work proberly (transform) 2 Answers
Disabling gravity when colliding with the terrain? 0 Answers
Stopping objects with colliding with similar objects. 1 Answer
Jumping and reverse gravity with a Rigidbody2D (based on the Ruby tutorials) 1 Answer
How to move a Game Object from a script not attached to it. 1 Answer