Skip to content

Instantly share code, notes, and snippets.

@st4rdog
Last active May 26, 2023 13:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save st4rdog/82a4d99c4f6eb59efa162a05ec62163b to your computer and use it in GitHub Desktop.
Save st4rdog/82a4d99c4f6eb59efa162a05ec62163b to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class ShakeTransformS : MonoBehaviour
{
[Header("Info")]
private Vector3 _startPos;
private float _timer;
private Vector3 _randomPos;
[Header("Settings")]
[Range(0f, 2f)]
public float _time = 0.2f;
[Range(0f, 2f)]
public float _distance = 0.1f;
[Range(0f, 0.1f)]
public float _delayBetweenShakes = 0f;
private void Awake()
{
_startPos = transform.position;
}
private void OnValidate()
{
if (_delayBetweenShakes > _time)
_delayBetweenShakes = _time;
}
public void Begin()
{
StopAllCoroutines();
StartCoroutine(Shake());
}
private IEnumerator Shake()
{
_timer = 0f;
while (_timer < _time)
{
_timer += Time.deltaTime;
_randomPos = _startPos + (Random.insideUnitSphere * _distance);
transform.position = _randomPos;
if (_delayBetweenShakes > 0f)
{
yield return new WaitForSeconds(_delayBetweenShakes);
}
else
{
yield return null;
}
}
transform.position = _startPos;
}
}
@debodhk76
Copy link

This code is working when I am applying this script onto my car body. But there is a problem. when I press the gas pedal the body remains stationary and the wheels move in the forward direction. Maybe because uneven contact between wheel colliders and box collider (of car body) during the shaky effect of the box collider ... I need a solution for this .. pls help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment