Last active
May 26, 2023 13:06
-
-
Save st4rdog/82a4d99c4f6eb59efa162a05ec62163b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Thank you very much
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
The code is great, the code is exactly what I needed, Thank you very much !!!