Search Unity

Shake an object from script

Discussion in 'Scripting' started by Mars91, Jun 2, 2012.

  1. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    572
    There's someone who can help me shaking an object using script?
     
    mmmshuddup likes this.
  2. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    Code (csharp):
    1.  
    2. var speed = 1.0f; //how fast it shakes
    3. var amount = 1.0f; //how much it shakes
    4.  
    5. function Update()
    6. {
    7.   transform.position.x = Mathf.Sin(Time.time * speed) * amount;
    8. }
    9.  
     
    Last edited: Mar 20, 2018
  3. harko12

    harko12

    Joined:
    Jul 6, 2012
    Posts:
    10
    Worked great for me, Thanks!
     
  4. tataygames

    tataygames

    Joined:
    Aug 4, 2016
    Posts:
    55
    thank you! :D
    the only problem is it only vibrates in 0 axis.
     
    Last edited: Nov 30, 2018
  5. tataygames

    tataygames

    Joined:
    Aug 4, 2016
    Posts:
    55
    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) );

    }
     
    YousifRagab, _protagonist and QQQ_QQQ like this.
  6. BendyBoi

    BendyBoi

    Joined:
    Mar 29, 2019
    Posts:
    1
    I've found many spelling errors in this.
     
  7. tataygames

    tataygames

    Joined:
    Aug 4, 2016
    Posts:
    55
    the logic is whats important
     
  8. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    Here's one:
    https://gist.github.com/st4rdog/82a4d99c4f6eb59efa162a05ec62163b

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ShakeTransformS : MonoBehaviour
    5. {
    6.    [Header("Info")]
    7.    private Vector3 _startPos;
    8.    private float _timer;
    9.    private Vector3 _randomPos;
    10.  
    11.    [Header("Settings")]
    12.    [Range(0f, 2f)]
    13.    public float _time = 0.2f;
    14.    [Range(0f, 2f)]
    15.    public float _distance = 0.1f;
    16.    [Range(0f, 0.1f)]
    17.    public float _delayBetweenShakes = 0f;
    18.  
    19.    private void Awake()
    20.    {
    21.        _startPos = transform.position;
    22.    }
    23.  
    24.    private void OnValidate()
    25.    {
    26.        if (_delayBetweenShakes > _time)
    27.            _delayBetweenShakes = _time;
    28.    }
    29.  
    30.    public void Begin()
    31.    {
    32.        StopAllCoroutines();
    33.        StartCoroutine(Shake());
    34.    }
    35.  
    36.    private IEnumerator Shake()
    37.    {
    38.        _timer = 0f;
    39.  
    40.        while (_timer < _time)
    41.        {
    42.            _timer += Time.deltaTime;
    43.  
    44.            _randomPos = _startPos + (Random.insideUnitSphere * _distance);
    45.  
    46.            transform.position = _randomPos;
    47.  
    48.            if (_delayBetweenShakes > 0f)
    49.            {
    50.                yield return new WaitForSeconds(_delayBetweenShakes);
    51.            }
    52.            else
    53.            {
    54.                yield return null;
    55.            }
    56.        }
    57.  
    58.        transform.position = _startPos;
    59.    }
    60.  
    61. }
    You have to call Begin() to start it.
     
  9. Jigganis

    Jigganis

    Joined:
    Jun 19, 2019
    Posts:
    5
    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.
     
  10. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Wherever in your code you want to start the shaking behavior you would put
    Code (csharp):
    1. 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.
     
  11. Jigganis

    Jigganis

    Joined:
    Jun 19, 2019
    Posts:
    5
    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.
     
  12. apalix1

    apalix1

    Joined:
    Aug 7, 2020
    Posts:
    1
    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):
    1.                        
    2.       float x = obj.transform.position.x * Mathf.Sin(Time.time * .1f) * .1f;
    3.       float y = obj.transform.position.y;
    4.       float z = obj.transform.position.z;
    5.  
    6. // Then assign a new vector3
    7.       gameObject.transform.position = new Vector3 (x, y, z);
    8.  
     
    ntkog, imoqen and WallaceHS like this.
  13. debodhk76

    debodhk76

    Joined:
    Jan 16, 2023
    Posts:
    1
    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
     
  14. lip312

    lip312

    Joined:
    Mar 7, 2018
    Posts:
    1
    Works really fine! Worked better for me when I put
    _startPos = transform.position;
    inside the Begin() function instead of Awake