Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Aug 20, 2018 at 10:56 AM by FreekDS for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by FreekDS · Aug 19, 2018 at 10:24 PM · 2d gamemovement scriptpixel arttop-down

2D Top down physics with ground (slippery floor...)

Hello I'm quite new to the Unity engine, and I'm currently working on a 2D top down game. I'm using Tilemaps to draw my levels, but I have a question. I'd like to create a slippery effect on some places of the map (like an ice floor). I've found a way, but I'm wondering if there are better solutions to achieve this goal.

I'm currently doing the following:

  • Created a new Tilemap on top of my default ground to draw my Ice patches on

  • Added a Tilemap Collider2D to that Tilemap and made it a trigger

  • When I enter the trigger, I update the movement of my player

  • I'll add my code below

    using System.Collections; using System.Collections.Generic; using UnityEngine;

    public class MovementController : MonoBehaviour {

       [SerializeField]
         private float topSpeed = 2.0f;
     
         private bool facingRight = true;
         private bool facingDown = true;
         private bool onIce = false;
     
         private Rigidbody2D rbody;
         private Animator animator;
     
         void Start() {
             rbody = GetComponent<Rigidbody2D>();
             animator = GetComponent<Animator>();
             Physics2D.gravity = Vector2.zero;
             Debug.Log("Initialized Player");
         }
     
         void Update() {
             
         }
     
         void FixedUpdate() {
             updateMovement();
             updateAnimator();
             if (onIce && stoppedMoving()) {
                 //print("Player stopped moving");
             }
         }
     
         void updateMovement() {
     
             //Get input
             float moveHorizontal = Input.GetAxis("horizontal");
             float moveVertical = Input.GetAxis("vertical");
     
             //Define wheter moving horizontal and/or vertical
             bool vertical = Mathf.Abs(Input.GetAxisRaw("vertical")) > 0.1f;
             bool horizontal = Mathf.Abs(Input.GetAxisRaw("horizontal")) > 0.1f;
     
             //Update movement
             if (horizontal && vertical)
                 //rbody.velocity = new Vector2(moveHorizontal, moveVertical) * topSpeed * 3 / 4;
                 rbody.AddForce(new Vector2(moveHorizontal * topSpeed, moveVertical * topSpeed) * 3 / 4);
             else
                 rbody.AddForce(new Vector2(moveHorizontal * topSpeed, moveVertical * topSpeed));
             //rbody.velocity = new Vector2(moveHorizontal, moveVertical) * topSpeed;
             
     
             //Flip if needed
             if (moveHorizontal < 0 && facingRight)
                 flipHorizontal();
             else if (moveHorizontal > 0 && !facingRight)
                 flipHorizontal();
     
             //stop moving when needed too
             if (Mathf.Abs(Input.GetAxisRaw("horizontal")) < 0.1f && !onIce)
                 rbody.velocity = new Vector2(0, rbody.velocity.y);
             if (Mathf.Abs(Input.GetAxisRaw("vertical")) < 0.1f && !onIce)
                 rbody.velocity = new Vector2(rbody.velocity.x, 0);
     
         }
     
         void updateAnimator() {
     
             bool vertical = Mathf.Abs(Input.GetAxisRaw("vertical")) > 0.1f;
             bool horizontal = Mathf.Abs(Input.GetAxisRaw("horizontal")) > 0.1f;
     
             //Update animator
             //horizontal/vertical
             if (Mathf.Abs(Input.GetAxisRaw("vertical")) > 0.1f && Mathf.Abs(Input.GetAxisRaw("horizontal")) > 0.1f) {
                 animator.SetBool("horizontal", true);
                 animator.SetBool("vertical", false);
             }
             else {
                 animator.SetBool("horizontal", horizontal);
                 animator.SetBool("vertical", vertical);
             }
             //up/down
             if (facingDown && Input.GetAxisRaw("vertical") > 0.1f) {
                 animator.SetBool("down", false);
                 facingDown = false;
             }
             else if (facingDown && Input.GetAxisRaw("vertical") < -0.1f) {
                 animator.SetBool("down", true);
                 facingDown = true;
             }
             else if (!facingDown && Input.GetAxisRaw("vertical") < -0.1f) {
                 animator.SetBool("down", true);
                 facingDown = true;
             }
             else if (!facingDown && Input.GetAxisRaw("vertical") > 0.1f) {
                 animator.SetBool("down", false);
                 facingDown = false;
             }
         }
     
         void flipHorizontal() {
     
             facingRight = !facingRight;
             Vector3 localScale = transform.localScale;
             localScale.x *= -1;
             transform.localScale = localScale;
         }
     
     
         bool stoppedMoving() {
             return Mathf.Abs(rbody.velocity.x) < 0.05f && Mathf.Abs(rbody.velocity.y) < 0.05f;
         }
     
         void OnTriggerEnter2D(Collider2D other) {
             if (other.gameObject.tag == "Icy") {
                 print("Entered ice collider");
                 onIce = true;
             }
         }
     
         void OnTriggerExit2D(Collider2D other) {
             if (other.gameObject.tag == "Icy") {
                 print("Exited ice collider");
                 onIce = false;
             }
         }
     }
     
    
    
    
    
    
    
    
    
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
1

Answer by Trevdevs · Aug 19, 2018 at 10:43 PM

Rather than programming friction which is very tedious why not just use https://docs.unity3d.com/Manual/class-PhysicsMaterial2D.html

Just create a sprite for normal ground and another for ice then place them together in a level. And set the friction on the ice physics material to something like 0.1 or lower to get a slippery effect

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FreekDS · Aug 20, 2018 at 10:56 AM 0
Share

Thanks! It did the job

avatar image Mr_Cad · Jan 04, 2020 at 01:43 AM 1
Share

How can this be the solution? OP was saying a top down 2D game. And the floor has to be isTrigger. Whenthe player move onto the tile, it's not moving into the tile's collider. How can the object interacting with physics material?

Follow this Question

Answers Answers and Comments

105 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to access a tile? 1 Answer

2D custom sort axis not working at all 2 Answers

How can i normalize 2d Vectors? 2 Answers

How to 2D Topdown Sprite order management 0 Answers

Player Character No Longer Moving 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges