Roll/rotate the cube on it's edge - c#

How to Roll/rotate the cube on it's edge?(Like this)
I read a couple of articles and answers to the questions but still not what I need.All I learned ,is that I need to create a gameobject in the center of the Cube and 4 others on the pivots.Something like that
And what's next, should I use Quaternions or transform.Rotate?Is the hierarchy correct?

I was about to tell you to use an external 3D software to set the pivot point to the location in your screenshot but it looks like you want to do this with multiple pivot points so you will to use empty GameObjects to accomplish this.
From your screenshot, you seem to be on the right track.
1.Create empty GameObjects and position them in the edges you want to rotate the cube around then place them under the cube.
2.Use transform.RotateAround not transform.Rotate to rotate the cube. The first parameter should be the edge pivot point. The second parameter is the axis you want to rotate the cube against. The third one is the angle.
//cube to rotate
public GameObject cube;
//Assign dge pos from the editor
public Transform edgePivotPoint;
public float rotSpeed = 60f;
void Update()
{
cube.transform.RotateAround(edgePivotPoint.position, Vector3.back, rotSpeed * Time.deltaTime);
}
Note, if rotating the wrong way, try replacing Vector3.up with Vector3.down, Vector3.left, Vector3.right, Vector3.forward and Vector3.back. The one to use depends on your scene setup but trying them will get you to the one

Related

How do I get a GameObject to rotate and face away from a MouseClick location in Unity?

So in my Unity exercise, I have to rotate a GameObject to face away from where I clicked my mouse. Also, I can only rotate around the Y-axis i.e. the GameObject is only allowed to rotate either purely to the right or purely to the left, and cannot tip towards the ground at any point. Also, I've got to do this without RayCasting (I've already done it WITH RayCasting, so as an exercise, I've got to do it without). Here's the code I've written after multiple attempts but it doesn't seem to be effective enough:
Vector3 clickLocation = Input.mousePosition;
Vector3 clickWorldLocation = Camera.main.ScreenToWorldPoint(new Vector3(clickLocation.x, clickLocation.y, transform.position.x)); //the transform.position.x is just to add some depth
transform.LookAt(new Vector3(clickWorldLocation.x, transform.position.y, clickWorldLocation.z), Vector3.up);
This code works fine if my GameObject remains in its starting position, but fails if I move it to another location and attempt the same action. Could someone help me out please?
First, call Camera.main as infrequently as you can, as it uses an expensive operation (FindGameObjectsWithTag) and doesn't cache the result.
private Camera mainCamera;
void Awake()
{
mainCamera = Camera.main;
}
To answer your question, don't use ScreenToWorldPoint in this situation, because a depth isn't most easily calculated. You don't have anything meaningful to use for the z component, and transform.position.x makes no sense here.
Instead, create a Plane that you want to click into (such as the plane parallel to the view plane and intersecting the object), use ScreenPointToRay to turn the mouse position into a Ray, then find where the ray and plane intersect. Plane.Raycast is much, much faster than a physics raycast. Then, find the direction from the mouse world position to the object, and use Quaternion.LookRotation to turn that direction into a rotation you can assign to the object's rotation:
Ray mouseRay = mainCamera.ScreenPointToRay(Input.mousePosition);
Plane mousePlane = new Plane(mainCamera.transform.forward, transform.position);
float rayDistance;
if (mousePlane.Raycast(mouseRay, out rayDistance))
{
Vector3 mouseWorldPos = mouseRay.GetPoint(rayDistance);
// make the z component of mouseWorldPos the same as transform.position
mouseWorldPos.z = transform.position.z;
Vector3 awayFromMouseDir = transform.position - mouseWorldPos;
transform.rotation = Quaternion.LookRotation(awayFromMouseDir, Vector3.up);
}

Rotating 2D world within Unity

I am looking at developing a game within Unity by using C#
The game will take the gyroscope orientation and rotate accordingly to the direction of which the phone is rotated.
I am curious of how this would work, I understand how I would be able to read and update the gyroscope orientation however I am unsure on how to assign this to a world to rotate. There will be a player on world which will be the next challenge to prevent the player clipping through the world when it is rotated.
Hence the world should rotate around the players current location.
I currently have no code as I am in the process of designing this, however i am unable to get the logic of how to make this work within my head
Thankyou
If I get your objective right, I guess rotating the camera like in a Fist-Person game should be enough, and would be way simpler than rotating the whole world while keeping the player static.
This is how I would go about it, first I wouldn't rotate the world, as that is alot of objects to rotate, but if you really wanted to you could parent all of the world to a single game object, then rotate that object about an axis based off of the players position(See this: https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html).
The simplest way is to attach your main camera to your player(Drag and drop it on your Player Object in your hierchy), then rotate your player.
for an example you can test on your machine without the gyroscrope:
using UnityEngine;
public class rotatorScript: MonoBehaviour {
void Update() {
// Will Rotate based off of left/right arrows
float rotator = -Input.GetAxisRaw("Horizontal");
// Move up or down based off of up/down Arrows
float verticalDirection = Input.GetAxisRaw("Vertical");
// Do the actual movement... using space.self so it is based on the player not the world.
transform.Translate(Vector3.up * verticalDirection * Time.deltaTime * 5f, Space.self);
transform.Rotate(0f,0f,90f * Time.deltaTime * rotator);
}
}
This script would be attached to the player.

In unity script, how to "rotate" and "rotate to" around pivot position

in unity editor, when i enable "Pivot", gameobject will rotate around "pivot point" position, when i enable "Center", gameobject will rotate around "center point"
but if i use script to rotate, it always rotate around "center point", for ex, here is my scene:
I use following code:
void Start()
{
// transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90));
transform.RotateAround(transform.position, new Vector3(0, 0, 1), 90);
}
it rotate around "center point"
if object has collider, i can get pivot point with colider.bounds, if not, how should i do?
and even worse, in some case, i hope rotate to like set rotate in unity editor inspector, i use following code:
transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90))
I have no idea to adjust above code to rotate around pivot point
update
I think if i can "rotate", i can also impl "rotate to", so the key point is, how should i get "pivot point" that is used in RotateAround, to object has collider, I can get with colider.bounds with transform.position, to no collider object, how should i do?
The pivot point is the-same as the transform.position you're already using. If you don't like where the pivot point is or the location the rotation is rotating against, you have two options:
1. Create new empty GameObject. Move it to the position you want your GameObject to rotate around. Once satisfied by that location, make this new GameObject a child of the GameObject you will be rotating around. Use the position of this new GameObject as the pivot rotation point for the transform.RotateAround function.
Drag the empty GameObject to the customPivot slot in the script below. That should give you a new pivot point to rotate your GameObject around.
public Transform customPivot;
void Update()
{
transform.RotateAround(customPivot.position, Vector3.up, 20 * Time.deltaTime);
}
2. Open a 3D application and change the pivot point of your object, then save and re-import it into Unity.
Since this is just used to rotate object around, I suggest #1. If the problem is that the pivot point is not centered, I would suggest #2.

Display Object in front of player without considering its rotation

I can show another object in front of my player using this simple line of code
newPosition =player.transform.position+player.transform.forward * distance
but how can i restrict it to always show in the same position(but in front of player) no matter what is the rotation of my player?
When you rotate gameobject transform.forward always changes.Because of you are using local values. But you need the use word values for this.
newPosition =player.transform.position+Vector3.forward * distance

Unity Instantiated object only moves up

I have a Light_Spell script attached to a magic wand which is parented to a Razer Hydra hand object. The Light_Spell takes a prefab of a Light which is projected out of it when a button is pressed.
However the light is just moving up, no matter what way I rotate the hand object, it always goes up. I had it working but I changed some code around and can't remember how I got it working in the first place.
Here is the code I have so far:
//What happens when bumper is pressed
if (isSelectedSpell && SixenseInput.Controllers [0].GetButtonDown (SixenseButtons.BUMPER) && triggerIsPressed == false) {
Rigidbody instantiateProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
instantiateProjectile.position += Vector3.down * 20.0F;
}
I have tried setting Vector3 to up and forward and a whole set of different things. Any ideas on what I should do to make it match the rotation of where the hand is pointing and stuff?
Thanks
Consider using:
instantiateProjectile.position += -instantiateProjectile.transform.up * 20.0f;
Explanation:
Vector3.down is not relative to the transform's rotation, it is a 'down' in World space, that is to say it will always be going down according to the cardinal axes.
transform.up is relative to that transform's rotation. If the projectile is rotated, the axes used to obtain "up" are rotated too. We must use an inverted transform.up instead of transform.down because no transform.down exists.

Categories

Resources