Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active August 29, 2015 14:21
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 unitycoder/879a93583ff121ca57c7 to your computer and use it in GitHub Desktop.
Save unitycoder/879a93583ff121ca57c7 to your computer and use it in GitHub Desktop.
Rotate Sphere By Moved Distance
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment