- Home /
Is it possible to make a Icy/Slippery floor with code with a fixed velocity?
Hello everyone, I'm making a 2D Platformer for android and I'm trying to make an "Ice Level" where ground tiles have the tag "Ice". Btw I'm programming in c#.
I wrote my own PlayerController Script wich i could post if it's necessary.
So if the Player walks on the ground with tag "Ice" the velocity of my player should lower in a fixed amount over a fixed amount of time and slides till he stands still.
I've tried it now for days and don't get the effect I want, honestly I didn't even got it to work with any result.
Now to my question :
Is it possible to achive such a effect only with code when i have a fixed velocity? If yes could someone please help me with the coding or point me out how to achive such an effect?
My idea was to save the last MoveSpeed and multiply it with a "Slide speed" so the MoveSpeed would lower over time but I'm not sure if my code is wrong cause i got no effect.
Answer by KaspianR · May 18, 2018 at 09:09 AM
Do you have some deceleration, or does it stop immediately? If you do have some deceleration you could just multiply the decelerationspeed with 0.1 or whatever to get that effect. You could even do it with the aceleration as well if you wan't your character to take a bit longer to start running at ful speed. You could determine if the player is standing on ice with the following script:
int TouchingIce = 0;
void OnCollisionEnter2D (Collision2D object) {
if (object.gameObject.tag == "Ice") {
TouchingIce++;
}
}
void OnCollisionExit2D (Collision2D other) {
if (object.gameObject.tag == "Ice") {
TouchingIce--;
}
}
Now you can just look if TouchingIce is more than zero and then the Player is touching atleast on ice object and you could apply the effect! Good luck!
If this still doesn't work or doesn't make any sense, please just share your script so that I can take a look at it!
Thank you for your answer, with the detection of the "Ice" I've got no problem at all just with the behaviour on the ice with the slide effect. And yes the Player stops immediately.
Here is my Player Controller Script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : $$anonymous$$onoBehaviour {
public float moveSpeed;
private float moveVelocity;
public Rigidbody2D myRiBo;
public Transform groundCheck;
public float groundCheckRadius;
public Layer$$anonymous$$ask whatIsGround;
public bool isGrounded;
private Animator myAnim;
public Vector3 respawnPoint;
public Vector3 respawnRotation;
public float gravityScale;
public Level$$anonymous$$anager theLevel$$anonymous$$anager;
public float jumpSpeed;
private void Awake () {
if (PlayerPrefs.GetInt ("Continue") == 1) {
transform.position = new Vector3 (PlayerPrefs.GetFloat ("PlayerX"), PlayerPrefs.GetFloat ("PlayerY"), PlayerPrefs.GetFloat ("PlayerZ"));
}
}
// Use this for initialization
void Start () {
Input.multiTouchEnabled = true;
myRiBo = GetComponent<Rigidbody2D> ();
myAnim = GetComponent<Animator> ();
respawnPoint = transform.position;
respawnRotation = transform.eulerAngles;
gravityScale = myRiBo.gravityScale;
theLevel$$anonymous$$anager = FindObjectOfType<Level$$anonymous$$anager> ();
}
// Update is called once per frame
void Update () {
// Ground Check for the Player
isGrounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
myRiBo.velocity = new Vector2 (moveVelocity, myRiBo.velocity.y);
myAnim.SetFloat ("Speed", $$anonymous$$athf.Abs (myRiBo.velocity.x));
myAnim.SetBool ("Grounded", isGrounded);
// Check wich direction the Player should look
if (myRiBo.velocity.x > 0) {
transform.localScale = new Vector3 (1f, 1f, 1f);
} else if (myRiBo.velocity.x < 0) {
transform.localScale = new Vector3 (-1f, 1f, 1f);
}
}
public void $$anonymous$$ove (float moveInput) {
moveVelocity = moveSpeed * moveInput;
}
void OnCollisionEnter2D (Collision2D other) {
if (other.gameObject.tag == "Ice") {
icy = true;
}
}
void OnCollisionExit2D (Collision2D other) {
if (other.gameObject.tag == "Ice) {
icy = false;
}
}
}
If you could help me with the slide effect I would be so thankfull.
And yes I want the effect you described. When the player is running and stops on ice he should slide until the speed is zero and when he wants to run again it should take some time until the player is at full speed.
Sorry for the double reply but I can't edit my first one.
Your answer
![](https://koobas.hobune.stream/wayback/20220612162903im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Inconsistent jump height 1 Answer
Inconsistent jump height 1 Answer
2D multi touch 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers