Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save unitycoder/879a93583ff121ca57c7 to your computer and use it in GitHub Desktop.

Select an option

Save unitycoder/879a93583ff121ca57c7 to your computer and use it in GitHub Desktop.

Revisions

  1. unitycoder revised this gist May 20, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RotateByDistance.cs
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ void Update () {
    transform.Translate(new Vector3(distance,0,0),Space.World);

    // rotate based on distance
    float angle=((distance*180)/(radius*Mathf.PI));
    float angle=(distance*180)/(radius*Mathf.PI);
    transform.eulerAngles += new Vector3(0,0,-angle);

    }
  2. unitycoder created this gist May 20, 2015.
    30 changes: 30 additions & 0 deletions RotateByDistance.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    using UnityEngine;
    using System.Collections;


    public class RotateByDistance : MonoBehaviour {

    public float moveSpeed = 2f;
    float radius = 0.5f;

    void Start ()
    {
    // get sphere size from collider
    radius = GetComponent<SphereCollider>().radius;
    }


    void Update () {

    // get input
    float distance = Input.GetAxis("Horizontal")*moveSpeed*Time.deltaTime;

    // move
    transform.Translate(new Vector3(distance,0,0),Space.World);

    // rotate based on distance
    float angle=((distance*180)/(radius*Mathf.PI));
    transform.eulerAngles += new Vector3(0,0,-angle);

    }
    }