Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

wiggle position

Discussion in 'Scripting' started by gringofxs, Jun 19, 2019.

  1. gringofxs

    gringofxs

    Joined:
    Oct 14, 2012
    Posts:
    240
    I need to wiggle position slowly and with a little amplitude of movement. there is any script to do that?
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    DOTween is a good library that allows you to tell an object how to animate with a single command and have it occur over a period of time. It's good for moving, rotating, scaling, and even some more component-specific things like fading an image. https://assetstore.unity.com/packages/tools/animation/dotween-hotween-v2-27676

    Otherwise, you can use a sine wave to give an object a smooth wave-like motion over time. Here's an example that wiggles up and down in a smooth wave-like motion.
    Code (CSharp):
    1. float wiggleDistance = 1;
    2. float wiggleSpeed = 1;
    3.  
    4. void Update()
    5. {
    6.     float yPosition = Mathf.Sin(Time.time * wiggleSpeed) * wiggleDistance;
    7.     transform.localPosition = new Vector3(0, yPosition, 0);
    8. }
     
  3. gringofxs

    gringofxs

    Joined:
    Oct 14, 2012
    Posts:
    240
    How can i complete this code that you giveme, please.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class fff : MonoBehaviour {
    6.  
    7. float wiggleDistance = 1;
    8. float wiggleSpeed = 1;
    9. void Update()
    10. {
    11.     float yPosition = Mathf.Sin(Time.time * wiggleSpeed) * wiggleDistance;
    12.     transform.localPosition = new Vector3(0, yPosition, 0);
    13. }}  
    14.  
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Start here:
    https://learn.unity.com/project/beginner-gameplay-scripting
     
  5. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Another, IMHO more lifelike 'wiggling' is by using perlin noise. I use it to animate the 'idle' position of a hovering spacecraft or hovertank. it wiggles up/down and left/right. Since I'm using perlin noise, there is no jarring move, and it Looks quite natually. Use the 'amplitude' vars to control the amount of wiggle, and the frequency variable to control the speed.

    Code (CSharp):
    1. public GameObject objectToBob;
    2. public float amplitudeX = 1f; // set to 0 if no x bob
    3. public float frequencyX = 1f; // boobing speed in x
    4. public float amplitudeY = 1f; // set to 0 if no y bob
    5. public float frequencyY = 1f; // bobbing speed in y
    6.  
    7. private Vector3 refPos;
    8.  
    9. public void Start(){
    10.   if (objectToBob == null) {
    11.       objectToBob = this.gameObject;
    12.   }
    13.   refPos = objectToBob.transform.position;
    14. }
    15.  
    16. public void Update(){
    17.   float dx = amplitudeX * (Mathf.PerlinNoise(Time.time * frequencyX, 1f) – 0.5f);
    18.   float dy = amplitudeY * (Mathf.PerlinNoise(1f, Time.time * frequencyY) – 0.5f);
    19.   Vector3 pos = new Vector3(refPos.x , refPos.y, refPos.z);
    20.   pos = pos + objectToBob.transform.up * dy;
    21.   pos = pos + objectToBob.transform.right * dx;
    22.   objectToBob.transform.position = pos;
    23. }
    24.  
    25.  
     
    Last edited: Jun 20, 2019
    DDmeow, Munchy2007 and SparrowGS like this.
  6. gringofxs

    gringofxs

    Joined:
    Oct 14, 2012
    Posts:
    240
    this error
    Assets/refPos.cs(1,7): error CS1525: Unexpected symbol `GameObject', expecting `class', `delegate', `enum', `interface', `partial', or `struct'
    Assets/refPos.cs(17,74): error CS1056: Unexpected character `?'


     
  7. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    687
    You can't just paste that code into an empty class. The class needs all it's implementations such as
    Code (CSharp):
    1. using UnityEngine;
    It needs a name such as
    Code (CSharp):
    1. class Name : MonoBehaviour {
     
  8. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    you need to wrap this in a class, you can't just copy and paste some code in a raw document.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class myClass : MonoBehaviour {
    5.  
    6. //put the code here
    7.  
    8. }

    You should begin with some tutorial or basic lessons to get a global understanding of what is happening ;)
     
  9. gringofxs

    gringofxs

    Joined:
    Oct 14, 2012
    Posts:
    240
    like this?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class refPos : MonoBehaviour {
    5. public GameObject objectToBob;
    6. public float amplitudeX = 1f; // set to 0 if no x bob
    7. public float frequencyX = 1f; // boobing speed in x
    8. public float amplitudeY = 1f; // set to 0 if no y bob
    9. public float frequencyY = 1f; // bobbing speed in y
    10. private Vector3 refPos;
    11. public void Start(){
    12. if (objectToBob == null) {
    13. objectToBob = this.gameObject;
    14. }
    15. refPos = objectToBob.transform.position;
    16. }
    17. public void Update(){
    18. float dx = amplitudeX * (Mathf.PerlinNoise(Time.time * frequencyX, 1f) – 0.5f);
    19. float dy = amplitudeY * (Mathf.PerlinNoise(1f, Time.time * frequencyY) – 0.5f);
    20. Vector3 pos = new Vector3(refPos.x , refPos.y, refPos.z);
    21. pos = pos + objectToBob.transform.up * dy;
    22. pos = pos + objectToBob.transform.right * dx;
    23. objectToBob.transform.position = pos;
    24. }
    25. }
     
    Thibault-Potier likes this.
  10. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    Yes exactly.

    I just tested this and it works. Care though, as i had to replace the minus character on line 18 and 19 "-" .
     
  11. gringofxs

    gringofxs

    Joined:
    Oct 14, 2012
    Posts:
    240
    error here
    Assets/refPos.cs(10,17): error CS0542: `refPos.refPos': member names cannot be the same as their enclosing type
    Assets/refPos.cs(18,72): error CS1056: Unexpected character `?'
    Assets/refPos.cs(18,74): error CS1525: Unexpected symbol `0.5'
    Assets/refPos.cs(19,72): error CS1056: Unexpected character `?'
    Assets/refPos.cs(19,74): error CS1525: Unexpected symbol `0.5'
     
  12. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    687
    You've named the class refPos and also a variable in the class refPos and then set it equal to a Vector3

    Unity doesn't know which one you're referring too, change the name of the variable to something different.

    You should really learn the basics before attempting something larger than you can do. This is pretty basic knowledge that you need to have.
     
  13. gringofxs

    gringofxs

    Joined:
    Oct 14, 2012
    Posts:
    240
    Im no a programer, i do sometimes code with playmaker, i would like to have a help on this script. Cant make it work
     
  14. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
  15. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    687
    I'll help you, but you need to learn this in the future and definitely consider learning C#, because if you want any chance at making a game, doing it like you are now, you're going to struggle a lot, you'll become deterred and may even stop making games; all because you didn't take 5-10 minutes a day learning something new.

    Problem: It's convention to name classes with a capital letter at the beginning, and variables with a lower case letter like so:

    class ThisIsAClass

    variable thisIsAVariable

    We do this to prevent exactly what's happening here.

    I've formatted your code to make it easier to read:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class refPos : MonoBehaviour
    6. {
    7.  
    8.     public GameObject objectToBob;
    9.  
    10.     public float amplitudeX = 1f; // set to 0 if no x bob
    11.     public float frequencyX = 1f; // boobing speed in x
    12.     public float amplitudeY = 1f; // set to 0 if no y bob
    13.     public float frequencyY = 1f; // bobbing speed in y
    14.  
    15.     private Vector3 refPos;
    16.  
    17.     public void Start()
    18.     {
    19.         if (objectToBob == null)
    20.         {
    21.             objectToBob = this.gameObject;
    22.         }
    23.         refPos = objectToBob.transform.position;
    24.     }
    25.  
    26.     public void Update()
    27.     {
    28.         float dx = amplitudeX * (Mathf.PerlinNoise(Time.time * frequencyX, 1f) - 0.5f);
    29.         float dy = amplitudeY * (Mathf.PerlinNoise(1f, Time.time * frequencyY) - 0.5f);
    30.         Vector3 pos = new Vector3(refPos.x , refPos.y, refPos.z);
    31.         pos = pos + objectToBob.transform.up * dy;
    32.         pos = pos + objectToBob.transform.right * dx;
    33.         objectToBob.transform.position = pos;
    34.     }
    35. }
    Adding spaces between things makes it so much easier to understand. Programming isn't just getting it to work, it's about allowing others to understand what you've written too.

    This updated version should work with what you want:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ReferencePosition : MonoBehaviour
    6. {
    7.  
    8.     public GameObject objectToBob;
    9.  
    10.     public float amplitudeX = 1f; // set to 0 if no x bob
    11.     public float frequencyX = 1f; // boobing speed in x
    12.     public float amplitudeY = 1f; // set to 0 if no y bob
    13.     public float frequencyY = 1f; // bobbing speed in y
    14.  
    15.     private Vector3 refPos;
    16.  
    17.     public void Start()
    18.     {
    19.         if (objectToBob == null)
    20.         {
    21.             objectToBob = this.gameObject;
    22.         }
    23.         refPos = objectToBob.transform.position;
    24.     }
    25.  
    26.     public void Update()
    27.     {
    28.         float dx = amplitudeX * (Mathf.PerlinNoise(Time.time * frequencyX, 1f) - 0.5f);
    29.         float dy = amplitudeY * (Mathf.PerlinNoise(1f, Time.time * frequencyY) - 0.5f);
    30.         Vector3 pos = new Vector3(refPos.x , refPos.y, refPos.z);
    31.         pos = pos + objectToBob.transform.up * dy;
    32.         pos = pos + objectToBob.transform.right * dx;
    33.         objectToBob.transform.position = pos;
    34.     }
    35. }
    I made 1 change, and that was changing the name of the class to ReferencePosition, from refPos.
     
    alienorbit likes this.