Hey! I am trying to create a slippery dip effect that will boost my player and fling it away, but I don't really know how to create the effect. I posted an image at the bottom of the post, trying to make it clearer what I'm trying to accomplish! I tried to make the player slide but it didn't really work that well, since the character just stopped herself and the speed was too slow. When it comes to creating addForce, how could I make it at several points on one object? This is what I tried creating for sliding down: Code (CSharp): float move = Input.GetAxis("Horizontal"); if (onSlipperyDip) { float targetVelocityX = move * maxSpeed; float temp = Mathf.SmoothDamp(rigi.velocity.x, targetVelocityX, ref velocityXSmoothing, 0.8f); rigi.velocity = new Vector2(temp, rigi.velocity.y); } else rigi.velocity = new Vector2(move * maxSpeed, rigi.velocity.y); This is how I created a trampoline earlier, and thought maybe I can do this effect, but it effects wherever I thouch the object, and only goes into one direction. Code (CSharp): void OnCollisionEnter2D(Collision2D col) { if (col.gameObject.name == "Player") { controller = col.gameObject.GetComponent<PlayerController>(); controller.onGround = false; Vector2 jumpForce = new Vector2(0, impulse); col.gameObject.GetComponent<Rigidbody2D>().AddForce(jumpForce, ForceMode2D.Impulse); } }
First of all, I assume that you do have a physics material with a friction very close to zero on the slide... Secondly, I think part of the problem is the direction in which you set your velocity. It would be better to apply the velocity along the tangent of the surface point upon which your character currently is.
Thanks for the tip! I do have fricton set to 0 right now. Is there an easy way to get the tanget of the surface in a PolygonCollider2D? As far as I've understood, I need to find the normal first to calculate it, which is another task to find.
The normal is given to you through the Collision2D object during the OnCollisionEnter2D callback, in the ContactPoint2D in the "contacts" array. You could also use raycasts and get the normal from the returned RaycastHit2D object, or do multiple raycasts and average the normals for a smoother ride across sharp changes in angle. The Cross Product of two vectors will give you a vector perpendicular to both. So getting the cross product of the surface normal, and Vector3.forward, will give you a vector perpendicular to both, and parallel to the ground. See this example: http://answers.unity3d.com/answers/133724/view.html You don't need to do the second check against Vector3.up, because your game is 2D and the normal should never point the same direction as Vector3.forward. Then you can either use that vector for your velocity direction, or Project your current velocity onto that vector.
Thanks for the reply! I think I've somewhat managed to solve it with the tangens function! Just gotta polish it out I used it with raycasting for now
Here is the result! I calculated the tangens on collision enter, and added an impulse onto my character in the direction of the tangens * -1!
If you used *-1 because the tangent was making the character go the wrong way, that's because the Cross Product gives you left or right side vectors based on the order and orientation of the vectors you give it. Try swapping the parameter order and you won't have to multiply by -1, or keep the order but use Vector3.back instead of Vector3.forward.