- Home /
How to make ice effect, or skidding car effect?
I want to make a moon game, but I am having trouble with making an effect that makes it hard to turn. This is an example of what should happen as the result:
1 The player moves forward and accelerates for an amount of time
2 The player turns sharp
3 The player keeps skidding in the previous direction, while gaining momentum in the new direction
Here's my code.
The GetAxis parts inside the WASD code is my attempt to make skidding.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float accelerationSpeed;
public float decelerationSpeed;
private float [] speed = new float [4];
public float maxSpeed;
private float mouseX;
private float mouseY;
public float xSensitiv;
public float ySensitiv;
public float jumpForce;
private Vector3 [] lastDirection = new Vector3 [4];
private Vector3 [] movement = new Vector3 [5];
void FixedUpdate () {
if (Input.GetKey (KeyCode.W))
{
speed [0] += accelerationSpeed * Time.deltaTime;
speed [0] = Mathf.Clamp (speed [0], 0, maxSpeed);
if (Input.GetAxis ("Mouse X") < 0.125f && Input.GetAxis ("Mouse X") > -0.125f)
{
movement [0] = transform.forward * speed [0];
}
else
{
movement [0] = lastDirection [0];
}
}
else
{
speed [0] -= decelerationSpeed * Time.deltaTime;
speed [0] = Mathf.Clamp (speed [0], 0, maxSpeed);
}
if (Input.GetKey (KeyCode.A))
{
speed [1] += accelerationSpeed * Time.deltaTime;
speed [1] = Mathf.Clamp (speed [1], 0, maxSpeed);
if (Input.GetAxis ("Mouse X") < 0.125f && Input.GetAxis ("Mouse X") > -0.125f)
{
movement [1] = -transform.right * speed [1];
}
else
{
movement [1] = lastDirection [1];
}
}
else
{
speed [1] -= decelerationSpeed * Time.deltaTime;
speed [1] = Mathf.Clamp (speed [1], 0, maxSpeed);
}
if (Input.GetKey (KeyCode.S))
{
speed [2] += accelerationSpeed * Time.deltaTime;
speed [2] = Mathf.Clamp (speed [2], 0, maxSpeed);
if (Input.GetAxis ("Mouse X") < 0.125f && Input.GetAxis ("Mouse X") > -0.125f)
{
movement [2] = -transform.forward * speed [2];
}
else
{
movement [2] = lastDirection [2];
}
}
else
{
speed [2] -= decelerationSpeed * Time.deltaTime;
speed [2] = Mathf.Clamp (speed [2], 0, maxSpeed);
}
if (Input.GetKey (KeyCode.D))
{
speed [3] += accelerationSpeed * Time.deltaTime;
speed [3] = Mathf.Clamp (speed [3], 0, maxSpeed);
if (Input.GetAxis ("Mouse X") < 0.125f && Input.GetAxis ("Mouse X") > -0.125f)
{
movement [3] = transform.right * speed [3];
}
else
{
movement [3] = lastDirection [3];
}
}
else
{
speed [3] -= decelerationSpeed * Time.deltaTime;
speed [3] = Mathf.Clamp (speed [3], 0, maxSpeed);
}
mouseX += Input.GetAxis ("Mouse X") * xSensitiv * Time.deltaTime;
transform.rotation = Quaternion.Euler (0, mouseX, 0);
mouseY -= Input.GetAxis ("Mouse Y") * ySensitiv * Time.deltaTime;
mouseY = Mathf.Clamp (mouseY, -90, 90);
Camera.main.transform.localRotation = Quaternion.Euler (mouseY, 0, 0);
if (GetComponent <CharacterController> ().isGrounded == true)
{
movement [4] = Vector3.zero;
if (Input.GetKey (KeyCode.Space))
{
movement [4] = Vector3.up * jumpForce;
}
}
else
{
movement [4] += Vector3.down * -Physics.gravity.y * Time.deltaTime;
}
GetComponent <CharacterController> ().Move ((movement [0] + movement [1] + movement [2] + movement [3] + movement [4]) * Time.deltaTime);
lastDirection [0] = movement [0];
lastDirection [1] = movement [1];
lastDirection [2] = movement [2];
lastDirection [3] = movement [3];
}
}
Also, I am not fixed on this code. I know there is probably a more efficient way to do this, so I don't necessarily have to keep this method.
Answer by HD32 · May 05, 2015 at 06:12 PM
I would use the default ice physics material. But it may not have the exact effect you are looking for.
Your answer
Follow this Question
Related Questions
How to create icy floor? c# 1 Answer
Rigidbody Movement 1 Answer
Change the direction of velocity upon tapping a button!!?? 1 Answer
Calculate weighted mean based on direction 0 Answers
How to push object in direction a player is facing C#? 1 Answer