• 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 /
avatar image
Question by jebey2 · Apr 15, 2012 at 11:01 PM · floor

Icy/Slippery floor

I am making a game for a class and I am stuck on trying to create and icy/slippery floor.

My character is being controlled via a character controller script.

I am trying to create a slippery floor that acts in either of 2 fashions(which one I go with depends on my teamates and the complexity on implementation).

First style would be similar to Mario Bros. 3 on NES in that the character is harder to control. Starting and Stopping movement would be harder.

Second style would be that you lose control of the character once on this icy terrain. The path of motion would be a straight line from whatever direction you entered the icy terrain in. You would continue this path until the end of the icy terrain.

I really just dont know where to begin to try and implement this. I thought about the second method, and just applying force through a script. I would do this by 2 planes(an OnTriggerEnter and OnTriggerExit plane), but my character does not have a rigidbody attached to it, so I dont think that would work. For the first method, I tried changing the physics type of the floor to "Icy" but that didnt seem to do anything. Honestly, I just dont know where to start on this. Any suggestions?

Comment
aldonaletto
maraoz
olamanianstudios
FullMe7alJacke7

People who like this

4 Show 0
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

4 Replies

· Add your reply
  • Sort: 
avatar image

Answer by HomeSpunGames · Apr 15, 2012 at 11:21 PM

I believe that there is a friction value that can be changed in the character collider. You could place a trigger over the area you want to be icy, so when the player enters that trigger his/her friction value is reduced. You could also reduce the friction of the ground as well.

Hope this points you in the right direction!

Comment

People who like this

0 Show 3 · 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 jebey2 · Apr 16, 2012 at 03:06 AM 0
Share

I did change the friction value of the icy physics property to 0 and changed the physics of the floor to icy but it didnt seem to do anything. I will try to change the character and the floor and see if it makes some kind of difference.

avatar image bodec · Apr 16, 2012 at 06:28 AM 0
Share

have you tried adding the ice physic material to your floor.

avatar image nomisvdp · Mar 25, 2019 at 09:51 PM 0
Share

this only works for slopes

avatar image

Answer by aldonaletto · Apr 16, 2012 at 04:42 AM

The CharacterController ignores physics, thus modifying the physic material makes no difference.
To make this "slippery ice" effect, you must modify the character movement script. The easiest way is to store the velocity vector passed to Move in a member variable (variable defined outside any function) and make it follow the controls via Lerp: the speed at which the velocity follows the controls is proportional to the friction. This works fine in flat surfaces, but will not make the character slip at inclined surfaces.
The actual code depends on your current movement script. If it's your own script, maybe the code below can replace it - or at least be adapted to suit your needs (character script - includes detection of "Ice" triggers):

var speed: float = 6.0; var jumpSpeed: float = 8.0; var friction: float = 1.0; // 0 means no friction; private var curVel = Vector3.zero; private var velY: float = 0; private var character: CharacterController;

function Update(){ // get the CharacterController only the first time: if (!character) character = GetComponent(CharacterController); // get the direction from the controls: var dir = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); // calculate the desired velocity: var vel = transform.TransformDirection(dir) * speed;

// here's where the magic happens: curVel = Vector3.Lerp(curVel, vel, 5 friction friction * Time.deltaTime);

// apply gravity and jump after the friction! if (character.isGrounded){ velY = 0; if (Input.GetKeyDown("Jump")){ velY = jumpSpeed; } velY -= gravity Time.deltaTime; } curVel.y = velY; character.Move(curVel Time.deltaTime); }

function OnTriggerEnter(other: Collider){ if (other.name == "Ice") friction = 0.1; // set low friction }

function OnTriggerExit(other: Collider){ if (other.name == "Ice") friction = 1; // restore regular friction } To define a slippery region, create a trigger covering the desired area and name it "Ice". You may have as many "Ice" objects you want.

NOTE: If you're using the standard First Person Controller, you must replace the input control script with the script below: save it in some Assets subfolder, add it to the First Person Controller and remove the original FPSInputController script:

var friction: float = 1.0;

private var motor : CharacterMotor; var curVel = Vector3.zero;

// Use this for initialization function Awake () { motor = GetComponent(CharacterMotor); }

// Update is called once per frame function Update () { // Get the input vector from keyboard or analog stick var dir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); var vel = transform.TransformDirection(dir); // Apply the friction factor: curVel = Vector3.Lerp(curVel, vel, 5*friction*friction*Time.deltaTime); motor.inputMoveDirection = curVel; motor.inputJump = Input.GetButton("Jump"); }

function OnTriggerEnter(other: Collider){ if (other.name == "Ice") friction = 0.1; // set low friction }

function OnTriggerExit(other: Collider){ if (other.name == "Ice") friction = 1; // restore regular friction }

Comment
SrBilyon
maraoz
agamedesigner
HiddenMonk
JDelekto
FullMe7alJacke7
Shinobi_Lord

People who like this

7 Show 0 · 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

Answer by jebey2 · Apr 16, 2012 at 04:43 PM

Thank you Aldo!

I had the revelation last night about physics not effecting my character. I realized there is no collider or physics attached to my character model other than gravity. Therefore it never really interacts with the floor underneath it, therefore changing the physics of the floor would do nothing. I was unsure if sliding/slippery movement could be handled via the movement of the character(I was thinking there might be a way to delay movement input by half a second to make it less responsive to simulate being harder to move in the direction you desired). That movement script you posted is very similar to the one I have so it should be easy to adopt it to mine. I believe this will function the way I want it to but if not I am sure I will be back here with another question.

Again, thank you Aldo and others for your input.

Comment

People who like this

0 Show 0 · 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

Answer by spiritworld · Dec 10, 2017 at 12:29 PM

just implemented my own icy surface solution without Lerping and thought to share the idea:

 private Vector3 moveDirection = Vector3.zero;
 private Vector3 lastMoveDirection = Vector3.zero;
 private float moveSpeed = 2f;
 private float slideSpeed = 1.75f;
 private bool icy = false;
 
 public void Update() {
 
     // read inputs
     float h = Input.GetAxis("Horizontal");
     float v = Input.GetAxis("Vertical");
     Vector3 moveDirection = v * camera.forward + h * camera.right;
 
     float inputMagnitude = Mathf.Min(new Vector3(h, 0, v).sqrMagnitude, 1f);
     
     // store last direction when received some movement
     if(inputMagnitude > 0.225f) { 
         lastMoveDirection = moveDirection;
     }
     
     // add speed
     // keeps sliding when still, runs slowly when moving
     if(icy) {
         moveDirection = lastMoveDirection * slideSpeed;
     } 
     else {
         moveDirection *= moveSpeed;
     }
     
     // ...
     // apply
     controller.Move (moveDirection * Time.deltaTime)
 }
 
 void OnControllerColliderHit(ControllerColliderHit hit) {
     icy = hit.collider.CompareTag("Ice");
 }

the sliding is now constant, I would improve this by adding speed deceleration

Comment

People who like this

0 Show 0 · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

How do I fix my first person controller from falling through the ground? 2 Answers

Creating Floor friction best practices 0 Answers

How can I make a looping floor for 2D game ,How can I make looping floor for 2D game,How to make looping floor for 2D game 0 Answers

zombies and the player SUDDENLY(not a beginning but a middle of playing) falling down 1 Answer

Strange Physics behaviour by a rolling ball in a custom floor/table 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