I have a small game where you move a cube around some levels and I want to create an ice hazard that makes the player slide in a direction. I use the following code to do this,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class iceController : MonoBehaviour {
private IEnumerator coroutine;
bool go = false;
private Vector3 dest;
private void Update()
{
if(go)
{
GameObject player = GameObject.Find("cube");
CubeController cube = player.GetComponent<CubeController>();
if(cube.lastmov == "up") //slide the player in the direction they are moving
{
dest = cube.parent.transform.position + (Vector3.left);
}
if (cube.lastmov == "down")
{
dest = cube.parent.transform.position + (Vector3.right);
}
if (cube.lastmov == "right")
{
dest = cube.parent.transform.position + (Vector3.forward);
}
if (cube.lastmov == "left")
{
dest = cube.parent.transform.position + (Vector3.back);
}
cube.parent.transform.position = Vector3.Lerp(cube.parent.transform.position, dest, .2f);
}
}
private void OnTriggerEnter(Collider other)
{
GameObject player = GameObject.Find("cube");
CubeController cube = player.GetComponent<CubeController>();
go = true;
cube.input = false; //stop the player from moving while sliding
}
private void OnTriggerExit(Collider other)
{
GameObject player = GameObject.Find("cube");
CubeController cube = player.GetComponent<CubeController>();
go = false;
cube.input = true;
}
}
But for some reason this is giving me inconsictent results. sometimes it does exactly what I want it to do, but other times it will fling the cube across the map violently. Any ideas on why this is happening or how to fix it?
EDIT: It may not specifically have to do with Lerp. I attempted to do the same thing with transform.translate and ran into the same issue, so consider me baffled!