May
20
2015
20
2015
Rotating Sphere By The Moved Distance
An article by mgear
2 Comments
How to rotate sphere based on the distance its moving.
Source: *Angle formula solved by ‘Denamond‘
This file contains 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); | |
} | |
} |
Related Posts
2 Comments + Add Comment
Leave a comment
Recent posts
- Create Scene Thumbnail Image using OnSceneSaved & OnPreviewGUI
- webgl+javascript TTS
- Using Moonsharp (LUA) + Unity Webgl
- Using 3D gameobject prefabs with Unity Tilemap + NavMesh Surface
- Custom Unity Hub Project Template Preview Image/Video (using HTML+CSS in package description)
- Check if point is inside polygon 2D (using line intersection)
- UnityHub: Enable Create Project Button (without having to select Cloud Organization)
- UI: Make Tooltip background fit Text content length
- Pong-Wars in Unity
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub)
- UnityHub: Add support for custom project title (instead of folder name) or use ProductName
- Testing Unity MUSE AI
Recent Comments
@unitycoder_com
My TweetsSubscribe to Blog via Email
Tag Cloud
2d
3D
AI
algorithm
android
asset
build
color
custom
demo
editor
effect
error
fake
free
game
generator
greasemonkey
indie
javascript
light
line
ludumdare
mesh
paint
particles
physics
plugin
proto
prototype
script
sea
shader
shadow
sprite
terrain
texture
tutorial
ui
unity
vertex
visibility
water
waves
webgl
[…] Rotating Sphere by the moved distance […]
[…] Rotating Sphere by the moved distance […]