Spinning a cube while it translate along the z-axis C#

Hi

Well, I’m sorry to bother everyone with yet another trivial question - actually there is two !

In my script have a made a cube which spin once the user click on it:

using UnityEngine;
using System.Collections;

public class RotatingCube : MonoBehaviour {
	
	public float cubeSpeed = 50.0f;
	
	public bool isCubeActivated = false;
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		
		if(isCubeActivated == true){
		
			//transform.Rotate(new Vector3(0,1,0) * cubeSpeed * Time.deltaTime);
		
			//transform.Translate(new Vector3(0,0,1) * cubeSpeed * Time.deltaTime);
		
			transform.Translate(new Vector3(0,0,1) * transform.Rotate(new Vector3(0,1,0) * cubeSpeed * Time.deltaTime));
			
			
		
		}
	}
	
	void OnMouseDown (){
		
		isCubeActivated = true; //the bool: isCubeActivated become true once the mouse pointer clicks on the cube
		
	}
	
}

Then I decided that I wanted the cube to just take off along the z-axis while spinning, so I tried to combine the translation and rotation into one line of code - which ofcourse was doomed to fail as I basically don’t know WTF I’m doing !

I sense that the problem and the reason why I get errors might because I’m trying to make (instantiate) a new vector3 for the rotation, when all I basically want is the cube to translate along the vector3 while spinning.

Is my assumption correct ?

IF the answer is yes THEN how do I correct this ?

Also, please look at the two lines of code:

transform.Rotate(new Vector3(0,1,0) * cubeSpeed * Time.deltaTime);
		
transform.Translate(new Vector3(0,0,1) * cubeSpeed * Time.deltaTime);

When I made the first line then the cube would just rotate (as expected), but when I added the second line the cube started to go around in a huge circle - that was, for me, an utterly surprising experience and I must admit that huge laugh as I thought it was hilarious which probably has something to do with the position of camera which made the cube appear to go like 100mph. as it passed the camera.

My question in regards to the short story is: why did the cube start to go around in a circle and not just translate along the z-axis while spinning ?

Use Space.World in Translate rather than the default Space.Self.

–Eric

Hi

I read through the Scripting Reference section regarding Transform.translate

So I tried to replace:

transform.Translate(new Vector3(0,0,1) * cubeSpeed * Time.deltaTime);

with this instead

transform.Translate(new Vector3(0,0,1) * cubeSpeed * Time.deltaTime,Space.World);

the result was that the cube now translate along the z-axis while spinning.

However, what really puzzles me is that this:

transform.Translate(new Vector3(0,0,1),Space.World * cubeSpeed * Time.deltaTime);

produces an error - it might be due to my lack of programming knowledge, but it would just seem more right to put Space.World there since, from how I understand it works, it is a parameter to Vector3.

It’s not; it’s an enum. It has no numerical meaning, and must be passed to Translate as a parameter which tells it to use world space or object-relative space when doing the computations. You can see in the docs that the first overload for Translate takes a Vector3 and a Space, and if the Space isn’t specified, then it uses Space.Self.

–Eric

Hi

Thank you for taking your time to explain - I tried to google “what is an enum in c#” and the explanations I could find made my head explode !

So, I’ll guess that I’ll just have to remember how place Space.World in the right place and stop wondering about why it is where it is.

Enums are just a way to use words instead of magic numbers, purely for the sake of the programmer. They do evaluate to some number, but typically those numbers are meaningless. For example, say they used an int instead of an enum: “Transform.Translate(Vector3.right, 0)” where “0” would mean relative to the world, and “1” would mean relative to the object. You can clearly see that “0” and “1” are far less meaningful and understandable than “Space.World” and “Space.Self”. It could just as well be “999” and “42”, or “4” and “5”, or “true” and “false” (which themselves are basically enums, and would be a bit better than random numbers, but still not as descriptive as “World” and “Self”).

–Eric

Hi

I understand what you’re saying - it makes perfectly sense.

During my little research on what an enum is, I read that it is, as you wrote, made to help the programmer.

You also wrote sentence which caught my eye:

That would be the same as:

Transform.Translate(Vector3.right, Space.Self); - right and it looks neat I think - eventhough I would prefer something like:

Transform.Translate(Vector3.right), Space.Self); - I know we can leave out the Space.Self, so to give a better example:

Transform.Translate(Vector3(1,0,0), Space.World); - on top of my head this would work, but if we add things like a velocity and time to the code then it becomes “messy” again as they just get "thrown into the mix (from my point of view)

If Transform.Translate(Vector3(x,y,z), Space.World); is the “purest” form then we shouldn’t tamper with it, but instead leave it as it is and the add the other items after the and not in between.

So it would be something like the example I gave:

transform.Translate(new Vector3(0,0,1),Space.World * cubeSpeed * Time.deltaTime);

But I’ll guess it is just in the eye of the beholder

All I know is that I have LOADS of problems using the Script Reference, because they use examples ie. mixing the code, so I can’t immediately see or comprehend what is what, but I’ll guess it will come as I hopefully become more experienced.

a really easy way to do this is make the cube a child of a empty gameobject and translate the gameobject and rotate the cube individually.

That makes no sense at all since Space.World has no mathematical meaning. You can’t multiply it by anything. It only makes sense to multiply Vector3.right by cubeSpeed and Time.deltaTime since you are moving a particular distance. e.g., if cubeSpeed was 100 and Time.deltaTime was .02, then that’s equivalent to saying “Translate (Vector3(0, 0, 1 * 100 * .02))” or “Translate (Vector3(0, 0, 2))”.

–Eric