Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

How to make sine amplitude restricted exactly to given units?

Discussion in 'Scripting' started by Ultimate360, Jul 23, 2017.

  1. Ultimate360

    Ultimate360

    Joined:
    Oct 28, 2016
    Posts:
    27
    Hi guys, I need help... How to make sine amplitude restricted exactly to given units? For example: if the editor set amplitude to 10, that's expected to be 10 units going up and down, but the Mathf.Sin() go beyond that (10 units), unlike Mathf.PingPong(). How will I fix that?

    I attach a zip file below...

    Here are my testing scripts:

    BulletPattern.cs //make two GameObject and attach this; set one pattern to Sine, and one to Ping Pong.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BulletPattern : MonoBehaviour {
    5.  
    6.     public GameObject pingPongBullet;
    7.     public GameObject sineBullet;
    8.     public enum patternType {
    9.         PingPong,
    10.         Sine
    11.     }; public patternType pattern;
    12.     public float amplitude = 10;
    13.     public float frequency = 1;
    14.     private Vector3 movement;
    15.     private GameObject bulletCollector;
    16.  
    17.     void Start () {
    18.         bulletCollector = GameObject.FindGameObjectWithTag ("BulletCollector");
    19.     }
    20.  
    21.     void Update () {
    22.         movement = transform.position;
    23.  
    24.         if (pattern == patternType.PingPong) {
    25.             movement += (Mathf.PingPong ((Time.time * amplitude * frequency) + (amplitude / 2), amplitude) - (amplitude / 2)) * transform.forward;
    26.             Instantiate (pingPongBullet, movement, transform.rotation, bulletCollector.transform);
    27.         } else if (pattern == patternType.Sine) {
    28.             movement += (Mathf.Sin (Time.time * Mathf.PI * frequency) * amplitude) * transform.forward;
    29.             Instantiate (sineBullet, movement, transform.rotation, bulletCollector.transform);
    30.         }
    31.     }
    32. }
    Projectile.cs //Make a prefab bullet (small sphere); 1 for Sin, one for PingPong.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Projectile : MonoBehaviour {
    5.  
    6.     public float speed = 20f;
    7.     // Use this for initialization
    8.     void Start () {
    9.         Destroy (gameObject, 1f);
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.         transform.position += Vector3.left * speed *Time.deltaTime;
    15.     }
    16. }
    Thanks for the help... :D
     

    Attached Files:

    Last edited: Jul 24, 2017
  2. Ultimate360

    Ultimate360

    Joined:
    Oct 28, 2016
    Posts:
    27
    Is there no fix or tricks to do this? Well, I just found out that I just need to multiply sine amplitude to 0.497878f something to be near perfect to unit length...
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,559
    Mathf.PingPong:
    https://docs.unity3d.com/ScriptReference/Mathf.PingPong.html

    So it returns a value from 0 to length, in your case amplitude. Which you then subtract amplitude/2 from. Expected results are:

    -amplitude/2 -> +amplitude/2

    Mathf.Sin:
    https://docs.unity3d.com/ScriptReference/Mathf.Sin.html

    So it returns a value from -1 to 1, which you multiply by amplitude. Expected results are:

    -amplitude -> +amplitude

    So yeah... you get twice the range.

    This is normal because that's the shape of a sine curve, by definition:


    Not sure how you landed on multiplying by 0.497878f... 0.5f would suffice, because you are getting twice the range you desire. So halving it would rectify that.

    But eh, 0.497 is pretty close to half, so visually speaking it's roughly the same.
     
    Ultimate360 and Kiwasi like this.