- Home /
Vector3 NextPosition = Position / 1.1; or Vector3 NextPosition = Position * 0.99; ? 1. Position of object is (20,0,20) 2. I need to get (19,0,19)
How to calculate something like
Vector3 NextPosition = Position / 1.1; or Vector3 NextPosition = Position * 0.99; ?
It don't want to work. But i simply need for example: 1. Position of object is (20,0,20) 2. I need to get (19,0,19)
Is there a way to do this somehow?
Answer by Lachee1 · Jun 15, 2015 at 08:09 AM
is it because you forgot to add the F on your floats? so instead of Position / 1.1, it would be Position / 1.1F
I just wanted to post that answer as well but i've found your comment in the last second ^^. I converted it to an answer.
Even though this answer is the correct answer of the original question, it's probably not what the OP actually wants if you read the comments under Addyarb's answer.
Since the distance between two adjacent ringe in that maze is constant it doesn't make sense to use any kind of ratio / percentage as the distance-changed would get smaller when you get closer to the center.
$$anonymous$$oveTowards is probably the best solution. However you don't pass Time.deltaTime as step, but the distance between two rings. It should even work with a negative step value to move outwards.
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, Vector3.zero, distance);
This however will of course "jump" to the next ring, so it shouldn't be executed every frame but once when you want to go to the next ring.
I've tried
Vector3 Position = transform.position;
Vector3 NextPosition = new Vector3(transform.position.x - 1,transform.position.y, transform.position.z - 1);
float step = speed * Time.deltaTime;
if (Vector3.$$anonymous$$oveTowards (Position, NextPosition, step)) {
print ("$$anonymous$$ove Towards works");
GetComponent<ConstantForce> ().force = Vector3.$$anonymous$$oveTowards( transform.position*5, transform.position*5, Time.deltaTime*5);
}
But IF part doesn't work. Vector3 can't be 'boolean'
So i am trying something like
newDistance = Vector3.Distance(transform.position, Vector3.zero);
if (newDistance<startDistance) {
print ("$$anonymous$$ove Towards works");
GetComponent<ConstantForce> ().force = Vector3.$$anonymous$$oveTowards( transform.position*5, transform.position*5, Time.deltaTime*5);
}
startDistance = newDistance;
And it actually works
Answer by Addyarb · Jun 15, 2015 at 03:29 AM
float x = -1; //The x amount to add to the x axis.
float y = 0; //The y amount to add.
float z = -1; //The z amount to add.
//The above values are variable. You can delete these and set them in the inspector.
void Start(){
transform.position = new Vector3(transform.position.x + x,transform.position.y +y, transform.position.z + z);
}
Thnx! Ideas about this?
Vector3 Position = transform.position;
Vector3 NextPosition = new Vector3(transform.position.x - 1,transform.position.y, transform.position.z - 1);
//If object moves to next position, push it away with $$anonymous$$oveTowards
if (Vector3.$$anonymous$$oveTowards (Position, NextPosition, Time.deltaTime)) {
GetComponent<ConstantForce> ().force = Vector3.$$anonymous$$oveTowards( transform.position*2, transform.position*2, Time.deltaTime*2);
}
or
Vector3 Position = transform.position;
Vector3 NextPosition = new Vector3(transform.position.x - 1,transform.position.y, transform.position.z - 1);
//If object moves to next position, push it away with $$anonymous$$oveTowards
if (Position = NextPosition) {
GetComponent<ConstantForce> ().force = Vector3.$$anonymous$$oveTowards( transform.position*2, transform.position*2, Time.deltaTime*2);
}
It is unclear what you want. It seems first you want the object to be placed, for instance from (20,0,20) to (19,0,19) which can be done as such:
transform.position = new Vector3(19,0,19);
nothing else. If you have a translation, that is some kind of force or movement then you need to know that one vector and apply it:
transform.Translate(movement);
For instance, you are at (20,0,20) and you wish to move some (1,0,1) then this is the one. If you sue this in the update it will move by that amount each frame, you can use deltaTime but that is out of question scope.
Now if you want o move from a position to another position like from (20,0,20) to (19,0,19), you need to interpolate. This can be done in two ways using Lerp or $$anonymous$$oveTowards. It is dependent on how you want the movement to happen.
Lerp takes the distance between current and target then multiplies by the ratio and adds that to hte current position:
newPosition = Vector3.Lerp(current, target, ratio);
newPosition = (target - current) * ratio;
This makes movement smaller each run (if target is not moving) and makes it look like it moving and slowing down as it reaches the target. ratio is a value between 0 and 1, 0 being no movement and 1 being full distance.
newPosition = Vector3.$$anonymous$$oveTowards(current, target, step);
Now, regardless of the distance between current and target, the object will move by the amount od step. If step is greater than the distance then target is returned. This create a constant movement, no slow down at the end.
I simply want IF statement to run each time when object moves from Position(x,y,z) to (x-1,y,z-1)
or not exactly (x-1, y, z-1) , i actually need (x multiply 0.99,y, z multiply 0.99) , but it won't work
What would be the situation making the object to move? I mean is it falling with gravity? Do you trigger the movement? What makes it go from A to B?
Ok, my advice on this, place a trigger circle (or sphere if 3D) in the middle of the map. And this on it:
void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.tag == "Ball"){
// Call on to start moving, I guess it is meant to be the boats.
}
}
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Vector3 and Quaternion issues. 2 Answers
Use yAxis as a Vector3(0,0,yAxis) paramenter? 3 Answers
Relative Rotation 1 Answer
Need some help with to grap .x with a Raycasthit (c#) 1 Answer