May
20
2015

Rotating Sphere By The Moved Distance

rolling_sphere_by_distance

How to rotate sphere based on the distance its moving.

As in:
http://docs.autodesk.com/3DSMAX/12/ENU/3ds%20Max%202010%20Tutorials/files/WS1a9193826455f5ff-3a29af00119afd28e95-4398.htm

Source: *Angle formula solved by ‘Denamond


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);
}
}

 


2 Comments + Add Comment

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Github Join Discord Twitch Instagram BlueSky

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.