-
-
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; | |
} | |
} |
Anyone want to tell a C# newbie how to call Begin()? I'm a little lost, I'd be grateful to be able to actually use this seemingly well-developed script.
If you want it to run automatically, put Begin() inside Awake().
If you want to run it from another gameobject, then find the gameobject and do:
public GameObject gameobjectReference;
void SomeFunction()
{
var shaker = gameobjectReference.GetComponent<ShakeTransformS>();
shaker.Begin();
}
The code is great, the code is exactly what I needed, Thank you very much !!!
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
Anyone want to tell a C# newbie how to call Begin()? I'm a little lost, I'd be grateful to be able to actually use this seemingly well-developed script.