Skip to content

Instantly share code, notes, and snippets.

@josephbk117
Last active September 11, 2023 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save josephbk117/a9156915434c7d6e7803142a2d9e61c6 to your computer and use it in GitHub Desktop.
Save josephbk117/a9156915434c7d6e7803142a2d9e61c6 to your computer and use it in GitHub Desktop.
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