- Home /
teleport an object three quarters of its distance with the player after a certain time
Hello everyone, I know how calculate a distance beetween the object and the player but i dont know how teleport an object three quarters of its distance with the day after a certain time, then with the half, then with the quarter ... Can you please help?
Thanks in advance.
Very nice post , And also all the topics of this blog are very informational , So i have create an account here for get more information . Thanks for share this information togather . i am very thanks full to you . click here
Thanks .
Answer by tetramess · Feb 17, 2018 at 12:15 PM
So assume you have X and Y coordinates.
Player has coordinates (pX, pY).
Object has coordinates (oX, oY).
The distance between them is sqrt((pX-oX)^2 + (pY-oY)^2) (sqrt means square root)
To move an object somewhere, you have to calculate what his new position will be. If you want to move him by a quarter, then calculate his new position:
newX = pX +(pX-oX)/4; and newY = oY + (pY-oY)/4; And move the object there (access his transform component and change the x and y values). However be careful - if you use the same formula to calculate how much closer to move the object, then the second time you use it it will move him by a quarter of the new distance, not the old. If you want those increments to stay consistent, you have to make a new variable and assign the increment 'size' by which it will have to change the object's position every time.
Hi thank you so much for you answer. I have good understant what you have explain me and in theory it look will be good, but i have try and i find one litlle wrong but i dont know where it from : I have transcript and try that you have say me on script and test it :
" public class Test : $$anonymous$$onoBehaviour {
Transform player;
public float newX;
public float newZ;
[Range(1,100)]
public float divisor;
void Start () {
player = GameObject.FindGameObjectWithTag ("Player").transform;
}
void Update () {
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha3))
{
//Calcul de la nouvelle position de l'objet en X : pX = player en X oX := object en X
//nouveauX = pX + (pX-oX) / 4; et newY = oY + (pY-oY) / 4
newX = (player.position.x + (player.position.x -transform.position.x)) / divisor;
newZ = (player.position.z + (player.position.z -transform.position.z)) / divisor;
transform.position = new Vector3 (newX, transform.position.y, newZ);
print ( newX);
print ( newZ);
divisor--;
}
}
} "
And when i test it, the player have deplace but have deplace over player, i dont know well how explain (sorry i'm not english and my english so terrible) , so maybe with litlle draw it will more clear about the problem :
i dont know what i have wrong or miss understant :(
Your answer
Follow this Question
Related Questions
Help Cannot convert type 'PhotonPlayer' to 'UnityEngine.GameObject' 1 Answer
How to teleport the player to the location of an instantiated projectile? 1 Answer
transform.position not a member of system.type 1 Answer
teleport destination differs when player hits trigger from different positions 0 Answers
How to add a teleportation mechanic like ender pearls in Minecraft 0 Answers