Code (csharp): var speed = 1.0f; //how fast it shakes var amount = 1.0f; //how much it shakes function Update() { transform.position.x = Mathf.Sin(Time.time * speed) * amount; }
this One is perfect, fully gameObject shake, and it will starts at the axis of your gameObject not in zero Vector2 startingPos; void Awake () { startingPos.x = transform.position.x; startingPos.y = transform.position.y; } // Update is called once per frame void Update () { gamObject.tranform.position.x = startingPos.x + Mathf.Sin(Time.time * speed) * amount ); gamObject.tranform.position.y = startingPos.y + (Mathf.Sin(Time.time * speed) * amount) ); }
Here's one: https://gist.github.com/st4rdog/82a4d99c4f6eb59efa162a05ec62163b Code (csharp): 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; } } You have to call Begin() to start it.
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.
Wherever in your code you want to start the shaking behavior you would put Code (csharp): Begin(); It could be in code which is part of an explosion. It can be called from a button. It can be called when you have a dinosaur start walking. Etc, etc, etc.
So this is a globally-accessible function, huh? I just need the function name alone? Do I want to name it something other than Begin? And thank you.
I tried this, but it won't work in Unity 2019. I kept getting the error cannot modify the return value of 'Transform.position' because it is not a variable. To make it work I had to assign the x, y, z positions to float variables and then use a vector3 assignment as in the following ex. Code (CSharp): float x = obj.transform.position.x * Mathf.Sin(Time.time * .1f) * .1f; float y = obj.transform.position.y; float z = obj.transform.position.z; // Then assign a new vector3 gameObject.transform.position = new Vector3 (x, y, z);
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) ... I need a solution for this .. pls help
Works really fine! Worked better for me when I put _startPos = transform.position; inside the Begin() function instead of Awake