Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Various motion exmaples with sin & cos
/*Please do support www.bitshiftprogrammer.com by joining the facebook page : fb.com/BitshiftProgrammer
Legal Stuff:
This code is free to use no restrictions but attribution would be appreciated.
Any damage caused either partly or completly due to usage of this stuff is not my responsibility*/
#define _Example4
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MotionLogic : MonoBehaviour
{
public float speed = 1.0f;
[Range(0.5f, 3.5f)]
public float radius = 2.0f;
#if _Example2
[Range(-6.28f, 6.28f)]
public float yAxisOffset = 2.5f;
#endif
#if _Example3 || _Example4
[Range(0.5f, 15.0f)]
public float frequency = 10.0f;
#endif
private void Update()
{
float inputValue = Time.timeSinceLevelLoad * speed;
#if _Example1
transform.position = new Vector2(Mathf.Sin(inputValue) * radius, Mathf.Cos(inputValue) * radius);
#endif
#if _Example2
transform.position = new Vector2(Mathf.Sin(inputValue) * radius, Mathf.Cos(inputValue + yAxisOffset) * radius);
#endif
#if _Example3
transform.position = new Vector2(Mathf.Sin(inputValue) * (radius + Mathf.Cos(inputValue * frequency)), Mathf.Cos(inputValue) * (radius + Mathf.Cos(inputValue * frequency)));
#endif
#if _Example4
transform.position = new Vector2(Mathf.Sin(inputValue) * (radius + Mathf.Cos(inputValue + Mathf.Sin(inputValue * frequency))), Mathf.Cos(inputValue) * (radius + Mathf.Cos(inputValue + Mathf.Cos(inputValue * frequency))));
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.