Last active
August 29, 2015 14:21
-
-
Save unitycoder/879a93583ff121ca57c7 to your computer and use it in GitHub Desktop.
Rotate Sphere By Moved Distance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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