Rotate a cube by dragging it so that it fits on one side

Hello everybody! First i have to say sorry for my bad english, but i will try to explain what i want to do in my project and what’s my problem.

I have a block with a RigidBody, Mesh Collider and Script.
The block is in 3D, so it has four sides and I want to rotate it by dragging it with the mouse in axis X. No problem for that, but I need that the block, rotate until one side (showing one face), and then other side with the dragging.
For example: The block is showing the face A, then i drag the mouse from right to left and the block rotate in the same direction and sense until show the face B, then if i drag the mouse from left to right, the block should show the face A, no matter how much i drag, the block should rotate only until show one face.
Shouldn’t show a vertice.

Can you help me with this? I’m a begginer. This is my code right now:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FloorController : MonoBehaviour {

public float rotSpeed = 250f;

private float rotX;

void Start (){

}

void OnMouseDrag(){

rotX = Input.GetAxis (“Mouse X”);
transform.Rotate (Vector3.up, -rotX * rotSpeed * Mathf.Deg2Rad);
}

}

Thank you!!!

Well, I think you are saying you want to clamp the angle, so it doesn’t go more than 90 degrees from it’s current position.

What do you want to clamp? Probably the rotation in eulerangles

notice the example on that page, which changes the rotation from the input. You might be able to do something similar.

That might be one way of doing it.

I have more problems with rotations, which is not the same as in the examples of position.
Exact. I want that when doing the mouse drag, the block rotates from 0 to 90 degrees… from the current position to + or - 90 degrees according to the mouse direction.

Are you asking about an immediate turn of 90 degrees or a gradual rotation?

Does the +/- 90 limit come from the start or from each initial drag?

You didn’t mention the axis, but here’s an example of rotation on the y-axis, limited to -90, and 90.

float rot;

void OnMouseDrag() {
   float h = Input.GetAxis("Mouse X");
   rot += h;
   rot = Mathf.Clamp(rot, -90, 90);
   transform.rotation = Quaternion.Euler(0, rot, 0);
}

Sorry, it’s hard to explain when you do not speak the language.

here is an explanation with drawings —>

my code is currently:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cubeController : MonoBehaviour {
public float rotSpeed = 250f;
private float posX1;
private float posX2;
private float distancia;

void Start(){

}
void Update (){

if (Input.GetMouseButtonDown(0))
{
posX1 = Input.mousePosition.x;
}

if (Input.GetMouseButtonUp(0)){
posX2 = Input.mousePosition.x;

distancia = posX1 - posX2;

if( distancia > 0)
{
this.transform.Rotate (new Vector3 (0, 90));
}
else
{
this.transform.Rotate (new Vector3 (0, -90));
}
}
}

}

Sorry… here is the file explicacion hosted at ImgBB — ImgBB

Okay, well the code I posted requires a continuous drag to complete but will stay within the parameters.

I think I have a better idea of what you’re looking for now. Try doing :

  • declare a variable that will track your rotation.
  • adjust it by +/- 90, depending on the click+drag direction.
  • set the rotation to rotate towards the new rotation, based on your speed.
float rot = 0;
void Update() {
 // your code here..
if( distancia > 0) {
    rot += 90;
   }
else if(distancia < 0) {
   rot -= 90;
  }
rot = Mathf.Clamp(rot, -90, 90);
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0,rot, 0), rotSpeed * Time.deltaTime);

So, try that with your code that is already there, adding the ‘rot’ outside of the method, and not using the rotate that you currently have.

Please look over this page for how to post code properly (nicely) on the forums: Using code tags properly

I think you should at least try the earlier code methos5k posted because it will rotate as you drag. Your code won’t do anything until you let up on the mouse, then the cube will spin. If that’s what you want, fine, but it won’t have a tangible feel, like you are actually spinning the cube. If I did it that way, I would just have left/right buttons to push. If you want it to actually feel like you are rotating the cube, you have to use OnMouseDrag like the earlier code.
From your original description, it sounds a lot more like you want the original code he posted.

You could write your own OnMouseDrag with booleans, etc, but that doesn’t make a lot of sense.

Hello,

I think you want to rotate a 3d object on one axe, if that is the case then you have to declare a float for the rotation speed
the use transform.RotateAround(here the rotation pivot point, here the axe to rotate the object on, here Input.MoussePosition * speed * Time.deltaTime);

it works for me if u need a c# file tell me