- Home /
Vector3 math problem?
Got unpredictable results with the following script in C#
void FixedUpdate ()
{
//move to position endpoint
if (Input.GetKey ("z")) {
var pos = transform.position;
var endpoint = new Vector3 (5, 5, 5);
transform.position = endpoint - pos; }
//come back from position
if (Input.GetKey ("x")) {
var pos = transform.position;
var startpoint = new Vector3 (0, 0, 0);
transform.position = startpoint - pos; }
}
The code should move the object from whatever position it may be to endpoint when "z" is pressed (and stay there if z is pressed again) and move to startpoint if "x" is pressed (and stay there).
Problem_1:object at (0,0,0), keep pressed z, : object flickers very fast between origin and designated endpoint. Why is that? I would understand to flicker in place as FixedUpdate means once per frame the position is verified and if already at 5,5,5 the difference vector should be zero (no change in position).
Problem_2:object at (0,0,0), alternate pressing z and x: object gets to progresively further positions. Why? The vector difference should bring the object from where ever it may be to the designated positions.
Problem_3: continue some time alternating z and x and the object should get to positions theoretically unobtainable from the fixed coordinates. For example, how does it get to a transform x axis of 6 adding and subtracting zeros and fives? I would understand if it were a smooth movement, but in this case it's move from A to B in one frame.
Problem_4:sometimes, with slightly larger code, pressing the keys does not produce any result (about one in 4 keystrokes gets the result). Why? It's as if the action must accur between a specific interval inside each frame or be ignored. Long and hard keystrokes seem to improve the rate. Is there a "pressed" value to diminish to make the scrip more responsive?
That's about it. Thanks. PS. Hope this get's posted
Answer by zharik86 · Nov 08, 2014 at 07:43 AM
You incorrectly work with vectors. The transform.position method returns an object position vector in world coordinates. If you only subtract a vector, anything at you it not to turn out. Let's look in numbers on your example with pressing of "x". The function FixedUpdate() is carried out each 0.1 second by default. For example, initially, your object had coordinates (5, 4, 3). Then at the first execution of a code: (0, 0, 0) - (5, 4, 3) = (-5,-4,-3). Further, second execution of a code: (0, 0, 0) - (-5,-4,-3) = (5, 4, 3). And, etc. Here from where blinking. I will write a code which will smoothly move object. And it is better to move it to the function Update() (write on CSharp):
public float mySpeed = 5.0f; //speed for smooth moving, if very fast, change value in Inspector
void Update () {
//move to position endpoint
//Better use KeyCode
if (Input.GetKey(KeyCode.Z)) {
Vector3 endpoint = new Vector3 (5, 5, 5);
//Smooth move, using Lerp function
transform.position = Vector3.Lerp(transform.position, endpoint, mySpeed * Time.deltaTime);
}
//come back from position
if (Input.GetKey(KeyCode.X)) {
Vector3 startpoint = new Vector3 (0, 0, 0);
//Smooth move, using Lerp function
transform.position = Vector3.Lerp(transform.position, startpoint, mySpeed * Time.deltaTime);
}
}
I hope that it will help you.
Thank you very much, zharik86 and tanoshimi. I can't believe I didn't try the simplest way first. Got into thinking vector3 was a force vector, hence the difference B-A to get a new force to get from A to B.
Case is utterly solved.
@$$anonymous$$-Greenhorn If I or everybody help you, please, mark my/him answer (below vote button).
Answer by tanoshimi · Nov 08, 2014 at 07:57 AM
If you just want to "teleport" the object straight to the two positions, you don't need to calculate the difference from the current location - just assign the destination position directly:
void Update ()
{
//move to position endpoint
if (Input.GetKey ("z")) {
transform.position = new Vector3 (5, 5, 5);
}
//come back from position
if (Input.GetKey ("x")) {
transform.position = new Vector3 (0, 0, 0);
}
}
Problems 1,2,3 are because you are assigning a vector difference, rather than either assigning the destination or adding the difference. Problem 4 is because you are using FixedUpdate instead of Update.
Your answer
![](https://koobas.hobune.stream/wayback/20220613171013im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
My gameobject moves up wrong 1 Answer
How to get a proper ramming effect? 1 Answer
Trouble converting transform.position to C# 1 Answer
How would I move an Object after collision? 1 Answer
How do I change one value of a vector? 2 Answers