Comments and answers for "How to make a Cube Roll in JS in direction of camera?"
http://answers.unity.com/questions/240520/how-to-make-a-cube-roll-in-js-in-direction-of-came.html
The latest comments and answers for the question "How to make a Cube Roll in JS in direction of camera?"Comment by PioBug on PioBug's answer
http://answers.unity.com/comments/1291768/view.html
I've translated the code do C# but the cube jumps off the plane if the keys are pressed for too long (it doesn't really stick to the plane even for the shortest press duration). The cube has a rigidbody and is using gravity.
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour {
public float speed = 2.5f; // flops per second
private float size = 1f; // block size
private Vector2 move;
private bool flopping = false;
private Vector3 pivot;
private Vector3 dir;
private void Update(){
move = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if (move.magnitude > 0.2f) {
Flop(move);
}
}
private IEnumerator Flop(Vector2 movDir){
if (flopping) {
yield return null; // ignore other commands while flopping
}
flopping = true; // signals it's flopping
if (movDir.y > 0)
{ // move forward?
dir = Vector3.forward; // will flop forward
pivot = new Vector3(0,-1,1); // defines point around which rotate
}
else if (movDir.y < 0){
dir = -Vector3.forward;
pivot = new Vector3(0,-1,-1);
}
else if (movDir.x < 0){
dir = -Vector3.right;
pivot = new Vector3(-1,-1,0);
}
else if (movDir.x > 0){
dir = Vector3.right;
pivot = new Vector3(1,-1,0);
}
AlignBlock(); // aligns block to grid before flopping
// calculates the point around which the block will flop
pivot = transform.position + (pivot * size * 0.5f);
Vector3 org = transform.position - pivot;
Vector3 dest = (transform.position + dir * size) - pivot;
Quaternion rot0 = transform.rotation;
Quaternion rot1 = Quaternion.FromToRotation(org, dest) * rot0;
float a = 0;
while (a < 1){
var dt = Time.deltaTime * speed;
a += dt;
transform.position = Vector3.Slerp(org, dest, a) + pivot;
transform.rotation = Quaternion.Lerp(rot0, rot1, a);
yield return null;
}
//if (audio) audio.Play(); // makes the flop sound
flopping = false;
}
private void AlignBlock(){
Vector3 angles = transform.eulerAngles;
// forces euler angles to be multiple of 90
angles.x = 90 * Mathf.RoundToInt(angles.x / 90);
angles.y = 90 * Mathf.RoundToInt(angles.y / 90);
angles.z = 90 * Mathf.RoundToInt(angles.z / 90);
transform.eulerAngles = angles;
Vector3 pos = transform.position;
// forces x and z to be in a grid
pos.x = size * Mathf.RoundToInt(pos.x / size);
pos.z = size * Mathf.RoundToInt(pos.z / size);
if (!GetComponent<Rigidbody>()) pos.y = size * Mathf.RoundToInt(pos.y / size);
transform.position = pos;
}
}Thu, 29 Dec 2016 12:49:11 GMTPioBugComment by DaiDesign on DaiDesign's answer
http://answers.unity.com/comments/431173/view.html
How would I change the direction of the box based on camera rotation.
Example:
If I rotate the camera left 90 degrees, How can I make the box still move away from camera when pressing W. So the box always moves away from camera when W is pressed no matter camera rotation camera is.Thu, 04 Apr 2013 00:53:55 GMTDaiDesignComment by DaiDesign on DaiDesign's answer
http://answers.unity.com/comments/422742/view.html
You Sir, get a virtual handshake and a pat on the back, but in all honesty, this is PERFECT for what I need.
Please provide your name in case this game idea I'm working on does go to market, I at least need to credit those people who have help.Sat, 23 Mar 2013 00:28:01 GMTDaiDesignComment by Bunny83 on Bunny83's answer
http://answers.unity.com/comments/422715/view.html
I've quickly build another <a href="https://dl.dropbox.com/u/7761356/UnityAnswers/Web/RollingCube/build.html">webplayer</a> to see it in action ;)Fri, 22 Mar 2013 23:08:37 GMTBunny83Comment by Bunny83 on Bunny83's answer
http://answers.unity.com/comments/422713/view.html
I've posted an answer to an even older question <a href="http://answers.unity3d.com/questions/144830/how-to-rotate-around-object-axis.html">here</a>. I've just took a look at my code and it's very simple to make it work with different sizes as long as the cube is aligned with the world because the easiest way to determine it's size is using renderer.bounds.size ;)
Btw: mine does even work with a quader with the size 1 x 2 x 3 ;)
Here's the modified and even simplified version of my script:
private var rotator : Transform;
var speed = 1.0;
var rotating = false;
function RotateCube(refPoint : Vector3, rotationAxis : Vector3)
{
var size = renderer.bounds.extents;
refPoint = Vector3.Scale(refPoint - Vector3.up,size);
rotator.localRotation = Quaternion.identity;
rotator.position = transform.position + refPoint;
transform.parent = rotator;
var angle : float = 0;
while(angle < 90.0)
{
angle += Time.deltaTime*90.0*speed;
rotator.rotation = Quaternion.AngleAxis(Mathf.Min(angle,90.0),rotationAxis);
yield;
}
audio.PlayOneShot(audio.clip);
transform.parent = null;
rotating = false;
}
function Start()
{
rotator = (new GameObject("Rotator")).transform;
}
function Update ()
{
if (!rotating)
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
{
rotating = true;
RotateCube(Vector3.right,-Vector3.forward);
}
else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
{
rotating = true;
RotateCube(-Vector3.right,Vector3.forward);
}
else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))
{
rotating = true;
RotateCube(Vector3.forward,Vector3.right);
}
else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.S))
{
rotating = true;
RotateCube(-Vector3.forward,-Vector3.right);
}
}
}Fri, 22 Mar 2013 23:04:37 GMTBunny83Comment by DaiDesign on DaiDesign's answer
http://answers.unity.com/comments/422682/view.html
How could this script be changed to roll a box with the dimensions of: 1 x 1 x 2, instead of a 1 x 1 x 1 cube.
I'm not a coder and I am just getting started with Unity, I am going to learn C# over time, hoping by --> trying <-- to make a simple puzzle game, I will hope to pick it up slowly.Fri, 22 Mar 2013 21:48:34 GMTDaiDesignComment by Free_Ko on Free_Ko's answer
http://answers.unity.com/comments/242644/view.html
I excually mean that if my camera is pointing forward that i can move backward with "S" and left and right with "A" & "D"Fri, 20 Apr 2012 15:06:16 GMTFree_KoComment by aldonaletto on aldonaletto's answer
http://answers.unity.com/comments/242641/view.html
It should go in the direction the camera is looking at - to move backwards, you should rotate the camera with the mouse to point the backward direction (the same applies to left and right).<br>
If you want to control the cube with the keyboard, just use the original code of this answer - its Update function reads "Horizontal" and "Vertical" axes to control cube movement.Fri, 20 Apr 2012 15:03:34 GMTaldonalettoComment by Free_Ko on Free_Ko's answer
http://answers.unity.com/comments/242616/view.html
I'v tried to make it move it backwards if this is poossible by changing the dir.x and dir.z but i couldn't work it out.
I alsoe want to make it move left and right but i might figure that out if i know how to move backwardsFri, 20 Apr 2012 13:48:36 GMTFree_KoComment by aldonaletto on aldonaletto's answer
http://answers.unity.com/comments/240590/view.html
I think you could get the direction camera->cube and fake a *move* vector that would make the cube move in the camera forward direction. But there's a problem: the cube will never stop moving, because there will always exist a forward direction. To solve this, define a button or key - the "walk" button.<br>
I assumed key W is the walk button, and modified Update to work this way - just replace the original Update with this one:
<pre>
function Update(){
if (Input.Get$$anonymous$$ey("w")){ // if W is pressed...
// get the dir camera->cube:
var dir = transform.position - Camera.main.transform.position;
// ignore the less significant horizontal direction:
if (Mathf.Abs(dir.z) >= Mathf.Abs(dir.x)){
dir.x = 0;
} else {
dir.z = 0;
}
Flop(Vector2(dir.x, dir.z));
}
}
...
</pre>Sun, 15 Apr 2012 22:49:07 GMTaldonalettoComment by Free_Ko on Free_Ko's answer
http://answers.unity.com/comments/240549/view.html
Thanks for the scrip you rlly helped me out. but i have one more question im useing the mouse orbit script and like to change the forward direction to the direction the camera is aiming.
i hope its possibleSun, 15 Apr 2012 21:17:36 GMTFree_KoAnswer by aldonaletto
http://answers.unity.com/answers/240525/view.html
I answered a question some time ago that did about the same thing: a cube that flipped in the direction defined by the arrow keys. The idea was to emulate the behaviour of [the game Edge][1].<br>
If you just want to reproduce the flipping walk style, the script below can do the job. Just create a cube and attach this script to it. If you want the flop sound, add an AudioSource and assign an appropriate sound to it (*footstep4* from the FPS Tutorial is fine).<br>
When some movement key is pressed, the routine *Flop* defines around which point and axis the block will rotate, then does a 90 degrees rotation during the time defined by *speed*. To avoid accumulated errors, the routine *AlignBlock* is called prior moving. This routine forces orientation alignment to the axes by rounding the eulerAngles to multiples of 90 degrees. It also forces the position alignment to multiples of the block size (defined by *size*) thus the block will always walk in a grid with *size* spacing.<br>
If the block has no rigidbody, it will be a really nice guy, flopping precisely over the grid positions - but will not be constrained by collisions nor be affected by gravity. If you add a rigidbody, it will collide and fall, but the flopping action will become somewhat weird (it sometimes stops for a while before ending the 90 degrees rotation). Anyway, the *AlignBlock* routine will always bring it back to the right position before moving.<br>
**NOTE:** if a rigidbody is added, *AlignBlock* ignores Y to not interfere with gravity.
<pre>
var speed: float = 2.5; // flops per second
var size: float = 1; // block size
var flopping = false;
function Update(){
var move = Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if (move.magnitude > 0.2) Flop(move);
}
function Flop(movDir: Vector2){
var pivot: Vector3;
var dir: Vector3;
if (flopping) return; // ignore other commands while flopping
flopping = true; // signals it's flopping
if (movDir.y > 0){ // move forward?
dir = Vector3.forward; // will flop forward
pivot = Vector3(0,-1,1); // defines point around which rotate
}
else
if (movDir.y < 0){
dir = -Vector3.forward;
pivot = Vector3(0,-1,-1);
}
else
if (movDir.x < 0){
dir = -Vector3.right;
pivot = Vector3(-1,-1,0);
}
else
if (movDir.x > 0){
dir = Vector3.right;
pivot = Vector3(1,-1,0);
}
AlignBlock(); // aligns block to grid before flopping
// calculates the point around which the block will flop
pivot = transform.position + (pivot * size / 2);
var org = transform.position - pivot;
var dest = (transform.position + dir * size) - pivot;
var rot0 = transform.rotation;
var rot1 = Quaternion.FromToRotation(org, dest) * rot0;
var a: float = 0;
while (a < 1){
var dt = Time.deltaTime * speed;
a += dt;
transform.position = Vector3.Slerp(org, dest, a) + pivot;
transform.rotation = Quaternion.Lerp(rot0, rot1, a);
yield;
}
if (audio) audio.Play(); // makes the flop sound
flopping = false;
}
private function AlignBlock(){
var angles = transform.eulerAngles;
// forces euler angles to be multiple of 90
angles.x = 90 * Mathf.RoundToInt(angles.x / 90);
angles.y = 90 * Mathf.RoundToInt(angles.y / 90);
angles.z = 90 * Mathf.RoundToInt(angles.z / 90);
transform.eulerAngles = angles;
var pos = transform.position;
// forces x and z to be in a grid
pos.x = size * Mathf.RoundToInt(pos.x / size);
pos.z = size * Mathf.RoundToInt(pos.z / size);
if (!rigidbody) pos.y = size * Mathf.RoundToInt(pos.y / size);
transform.position = pos;
}
</pre>
[1]: http://www.youtube.com/watch?v=qNUcD-FXgDI&feature=player_embeddedSun, 15 Apr 2012 20:15:11 GMTaldonaletto