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.
using UnityEngine;
using System.Collections;
public class BulletPattern : MonoBehaviour {
public GameObject pingPongBullet;
public GameObject sineBullet;
public enum patternType {
PingPong,
Sine
}; public patternType pattern;
public float amplitude = 10;
public float frequency = 1;
private Vector3 movement;
private GameObject bulletCollector;
void Start () {
bulletCollector = GameObject.FindGameObjectWithTag ("BulletCollector");
}
void Update () {
movement = transform.position;
if (pattern == patternType.PingPong) {
movement += (Mathf.PingPong ((Time.time * amplitude * frequency) + (amplitude / 2), amplitude) - (amplitude / 2)) * transform.forward;
Instantiate (pingPongBullet, movement, transform.rotation, bulletCollector.transform);
} else if (pattern == patternType.Sine) {
movement += (Mathf.Sin (Time.time * Mathf.PI * frequency) * amplitude) * transform.forward;
Instantiate (sineBullet, movement, transform.rotation, bulletCollector.transform);
}
}
}
Projectile.cs //Make a prefab bullet (small sphere); 1 for Sin, one for PingPong*.*
using UnityEngine;
using System.Collections;
public class Projectile : MonoBehaviour {
public float speed = 20f;
// Use this for initialization
void Start () {
Destroy (gameObject, 1f);
}
// Update is called once per frame
void Update () {
transform.position += Vector3.left * speed *Time.deltaTime;
}
}
Thanks for the help… ![]()
3156082–240039–SineAndPingPong.zip (612 KB)
