- Home /
How to create icy floor? c#
Currently I'm working on a top view ball game and I would like to have icy floor parts in it. I plan to do it with using object and collider, but I have absolute no idea how to start. The object (with tag 'IceFloor') would be a few above the ground on y with a trigger on collider and when the player ball would go onto it it should behave like if it would be on ice.
Currently what I have is the ball with a controlling script which is simply moving the ball with arrow keys:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
void FixedUpdate () {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
GetComponent<Rigidbody>().AddForce (movement * speed * Time.deltaTime);
}
}
I don't have any Physic material yet, not even sure if needed, and if yes where and how. I'm very new to coding. I would really appriciate any help. It would really make me very happy it could work.
Answer by SpectreCular · Mar 13, 2015 at 09:14 AM
From http://docs.unity3d.com/Manual/class-PhysicMaterial.html:
"Dynamic Friction - The friction used when already moving. Usually a value from 0 to 1. A value of zero feels like ice, a value of 1 will make it come to rest very quickly unless a lot of force or gravity pushes the object."
Create and apply a Physic material, and play around with the values. Namely, the one shown above.
Thanks for this hint. I managed to get something which is really close to what I want. I created a Physics $$anonymous$$aterial which has dynamic friction 1 on it and it is attached to the ball. In my player controller script added an OnTriggerEnter for object tag Icefloor and dynamic friction is being lowered to 0. It works just fine, as soon as my ball touches my icefloor object it behaves like on ice. I created an OnTriggerExit for the same tag which should raise the dynamic trigger back to 1. But this part just works strange. $$anonymous$$y ball behaves after leaving the icefloor like if it would be on a glued surface, starts to move very slowly, but only to direct directions(up,down,left,right). But if I try up-right then it is ok. I have no idea what is wrong. Here is my current code:
using UnityEngine;
using System.Collections;
public class PlayerController2 : $$anonymous$$onoBehaviour {
public float speed;
public float torque;
void FixedUpdate () {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
GetComponent<Rigidbody>().AddForce (movement * speed * Time.deltaTime);
GetComponent<Rigidbody>().AddTorque (Vector3.up * torque * moveHorizontal);
GetComponent<Rigidbody>().AddTorque (Vector3.up * torque * moveVertical);
}
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "IceFloor") {
GetComponent<Collider>().material.dynamicFriction = 0;
}
}
void OnTriggerExit(Collider other){
if (other.gameObject.tag == "IceFloor") {
GetComponent<Collider>().material.dynamicFriction = 1;
}
}
}
I changed the OnTriggerEnter to OnTriggerStay and removed the OnTriggerExit and now it is perfect!Thanks for helping out!
Your answer
Follow this Question
Related Questions
Simple 2D smooth ball movement 1 Answer
The ball is stuck in the beginning position 1 Answer
Rigidbody Movement 1 Answer
Ball Rolling System 1 Answer
The Physics of Ball Spin in Tennis, Topspin and Backspin 3 Answers