Skip to content

Instantly share code, notes, and snippets.

Last active September 11, 2023 14:17
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save josephbk117/a9156915434c7d6e7803142a2d9e61c6 to your computer and use it in GitHub Desktop.
Various motion exmaples with sin & cos
/*Please do support by joining the facebook page :
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;
#if _Example3 || _Example4
[Range(0.5f, 15.0f)]
public float frequency = 10.0f;
private void Update()
float inputValue = Time.timeSinceLevelLoad * speed;
#if _Example1
transform.position = new Vector2(Mathf.Sin(inputValue) * radius, Mathf.Cos(inputValue) * radius);
#if _Example2
transform.position = new Vector2(Mathf.Sin(inputValue) * radius, Mathf.Cos(inputValue + yAxisOffset) * radius);
#if _Example3
transform.position = new Vector2(Mathf.Sin(inputValue) * (radius + Mathf.Cos(inputValue * frequency)), Mathf.Cos(inputValue) * (radius + Mathf.Cos(inputValue * frequency)));
#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))));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment