- Home /
Move a cube on it's edges
Hey guys,
I'm trying to move a cube on a grid. So one move should look like an animation , where either x or y values are changed by +/- 1 and a rotation of 90 degree.
Anyway I couldn't find a good solution on this. I read something about animations, but I'm not sure if that's what I need and how I could differ between the directions.
Are there any ideas on this?
Additionally I couldn't fix my camera to my object yet (always above). I can't make the camera a child of my cube, because the cube is instantiated in code.
Any ideas on this?
Answer by gajdot · Nov 19, 2013 at 11:47 AM
First of all you need a follow script attached to the camera. Something like this:
camera.transform.position = new Vector3(target.transform.x, target.transform.y+5, target.tranform.z);
Something like this in the update function, to track the position of your cube.
Now about the rotation I need to think a bit and I have work atm. It needs to be done or adding torque or animation or just rotating an object, but I need to test which works best. When I get home I'll try it out for you and post my results.
The problem with this attempt is, that I also instantiate the Cube in my code. So in the Scene there is nothing but the camera.
I now did an animation for my cube to move on key input. The problem i am facing now, is that the animation will always reset the object to the animations origin. How could I achieve it to be relative?
Well one hack you could do is that, you can translate your object as soon as your animation stops to the new location, so it would seem it will remain there.
That's what I already tried to do, I guess animation.Play("move_forward", Play$$anonymous$$ode.StopAll); transform.Translate(Vector3.forward, Space.World); But I recorded my animation at 0,0,0. Although I Instantiate my cube at 5, 0, 5 it will jump to the origin at start of the animation. Also the cube will not be positioned as it would without the animation
I see, the problem is that you need a parent object positioning your cube with this solution. You would need a parent emptyGameobject witch would be the cube holder and record the animation on the children of this object witch would be your cube. This way if you position your parent it will bring your cube with it and use it's local origin. Try this or if you want I just arrived home and I can write you a functional script to rotate it, without animation.
I already tried this approach but I failed. But I also don't want to to put too much time in my project. I guess I will have to mention you in the credits ^^
Answer by tanoshimi · Nov 19, 2013 at 11:56 AM
Two ideas (both untested) to roll the cube:
Use transform.RotateAround, choosing the pivot point to be the centre of the edge lying on the floor in the direction you want to roll the cube (which should be relatively easy to find given you know the cube's dimensions and it's centre point).
Use Physics, giving the cube a material that is very high friction, high drag, and low bounce. Then apply a force to the cube - it should roll over rather than slide.
Hey, but do I have to know in which direction the cube currently is heading? Because if this is the case it sounds quite complicated to me.
Let's say I want to add the logic for rotating here:
if(Input.Get$$anonymous$$eyDown("w")){
transform.Translate(Vector3.forward, Space.World);
}
I also do not know how to apply a force according to my movement.
No you don't need to know which way the cube is "pointing" because it's a cube so it makes no difference - it's completely symmetric in all its movement axes. You only need to know which direction you want to push it in (e.g Vector3.Forward, Vector4.Right, -Vector3.Forward or -Vector3.Right). Calculate the pivot point from cube centre + width/2 translation in x/z axes (to find the "side" on which to rotate, - height/2 (to get the point in the middle of the bottom edge of that side)
Your answer
Follow this Question
Related Questions
Setting a cube to be exactly size of intersecting camera view plane 1 Answer
Roll a cube help 0 Answers
How to make camera position relative to a specific target. 1 Answer
How to lock camera roll in C# 1 Answer
How to make a "pressure sensor" of sorts that when rolled over switches to another scene 2 Answers