1

I want to know the direction a spaceship is actually moving to, its path of motion. I am doing it like so

void Start() { prevPos = transform.position; } void FixedUpdate() { Vector3 currPos = transform.position; Vector3 motion = currPos - prevPos; Debug.Log("Current position: " + currPos + " Previous position: " + prevPos + " Difference: " + motion); prevPos = currPos; } 

The problem is that each frame the ship doesn't move too much, and the difference between currPos and prevPos (I'm guessing) is in the decimals (0.0530 for example). From what I read Vectors go up to 7 decimals, yet motion equals (0, 0, 0) most frames.

In the Debug above I get these results

Current position: (0, 0, 0.5) Previous position: (0, 0, 0.4) Difference: (0, 0, 0)

2 Answers 2

1

The Debug.Log function is made to truncate Vector3 on purpose to make it more readable. If you need to show all decimals, simply make a function to concatenate and show each individual component of the Vector3.

void PrintVector3(Vector3 message, int type = 1) { if (type == 1) Debug.Log("X: " + message.x + " Y: " + message.y + " Z:" + message.z); if (type == 2) Debug.LogWarning("X: " + message.x + " Y: " + message.y + " Z:" + message.z); if (type == 3) Debug.LogError("X: " + message.x + " Y: " + message.y + " Z:" + message.z); } 

Also when you concatenate string with Vector3, you also lose the decimal places. You can make a simple function to concatenate string and Vector3 an return the string. This string you can then print with Debug.Log.

string Concat(string val, Vector3 vec) { return val + " (" + vec.x + ", " + vec.y + ", " + vec.z + ")"; } 
Sign up to request clarification or add additional context in comments.

4 Comments

Yeah. You should create new question with the problem. Also add the raycast code and explain what's happening and what you expect to happen.
With the Debug.ray I wanted it to go from current position to previous position, so I used those arguments (currPos, prevPos...) but the arguments of that function are (from, direction...) and not from, to...
@RealAnyOne to compute direction just normalize the vector to - from.
The Debug.Log function logs whatever object it is given by calling its ToString method. It's the Vector3.ToString method that does the rounding.
0

As SilentSin points out, Vector3.ToString() is being called for you. If you call ToString yourself, you can control the precision of the rounding.

Debug.Log("Current position: " + currPos.ToString("N4") + " Previous position: " + prevPos.ToString("N4") + " Difference: " + motion.ToString("N4")); Debug.LogFormat("Current position: {0} Previous position: {1} Difference: {2}", currPos.ToString("N4"), prevPos.ToString("N4"), motion.ToString("N4")); 

I find this useful for debugging as I can control the real estate claimed by each value.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.