- Home /
transform.Translate causing object to teleport.
My code: thiTra.Translate(0,-1,0);
The variable thiTra
is just there so that I don't have to type this.gameObject.transform
over and over again. It is a transform assigned to this object's transform.
I also have a delay so that it will only do that every second. It will start at (0.5,2.5,0) then translate to (0.5,1.5,0), then translate about seven or eight units downward. Why is this happening? I played around with it, and it happens whenever any corner hits 0,0,0. (It is scaled at 1,1,1) Please help!
EDIT: I am NOT expecting smooth movement! I don't care that it is stutter-stepping because I don't lack basic programming knowledge and I know how to fix it later! It just keeps randomly teleporting, and THIS is the problem! Not the stutter stepping. I am being so EXCLAMATORY about it because EVERYONE is giving me fixes for that! Please help me with the teleporting! Full script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FallScript : MonoBehaviour {
Transform thiTra;
GameObject GravCont;
float maxDelay = 0.3f;
float delay;
// Use this for initialization
void Start () {
GravCont = GameObject.Find("GravityController");
thiTra = this.gameObject.transform;
}
void Update () {
delay -= 0.1f;
if (delay <= 0) // If can do it
{
if (GravCont.GetComponent<GravityController>().dir == "u")
{ // Check direction of gravity (UP)
if (Physics.CheckSphere(new Vector3(thiTra.position.x, thiTra.position.y + 1, thiTra.position.z), 0.2f))
{ // If there is something in front of where I am trying to go
Collider[] founds = Physics.OverlapSphere(new Vector3(thiTra.position.x, thiTra.position.y + 1, thiTra.position.z), 0.2f); // Assign them
for (int i = 0; i < founds.Length; i++)
{ // See if they are walls
if (founds[i].gameObject.CompareTag("Wall"))
{
return;
}
if (founds[i].gameObject.CompareTag("Fall"))
{
return;
}
thiTra.Translate(0, 1, 0);
}
}
else
{ // if there is an empty space
thiTra.Translate(0, 1, 0);
}
}
if (GravCont.GetComponent<GravityController>().dir == "d")
{ // Check direction of gravity (UP)
if (Physics.CheckSphere(new Vector3(thiTra.position.x, thiTra.position.y - 1, thiTra.position.z), 0.2f))
{ // If there is something in front of where I am trying to go
Collider[] founds = Physics.OverlapSphere(new Vector3(thiTra.position.x, thiTra.position.y - 1, thiTra.position.z), 0.2f); // Assign them
for (int i = 0; i < founds.Length; i++)
{ // See if they are walls
if (founds[i].gameObject.CompareTag("Wall"))
{
return;
}
if (founds[i].gameObject.CompareTag("Fall"))
{
return;
}
thiTra.Translate(0, -1, 0);
}
}
else
{ // if there is an empty space
thiTra.Translate(0, -1, 0);
}
}
if (GravCont.GetComponent<GravityController>().dir == "l")
{ // Check direction of gravity (UP)
if (Physics.CheckSphere(new Vector3(thiTra.position.x - 1, thiTra.position.y, thiTra.position.z), 0.2f))
{ // If there is something in front of where I am trying to go
Collider[] founds = Physics.OverlapSphere(new Vector3(thiTra.position.x - 1, thiTra.position.y, thiTra.position.z), 0.2f); // Assign them
for (int i = 0; i < founds.Length; i++)
{ // See if they are walls
if (founds[i].gameObject.CompareTag("Wall"))
{
return;
}
if (founds[i].gameObject.CompareTag("Fall"))
{
return;
}
thiTra.Translate(-1, 0, 0);
}
}
else
{ // if there is an empty space
thiTra.Translate(-1, 0, 0);
}
}
if (GravCont.GetComponent<GravityController>().dir == "r")
{ // Check direction of gravity (UP)
if (Physics.CheckSphere(new Vector3(thiTra.position.x + 1, thiTra.position.y, thiTra.position.z), 0.2f))
{ // If there is something in front of where I am trying to go
Collider[] founds = Physics.OverlapSphere(new Vector3(thiTra.position.x + 1, thiTra.position.y, thiTra.position.z), 0.2f); // Assign them
for (int i = 0; i < founds.Length; i++)
{ // See if they are walls
if (founds[i].gameObject.CompareTag("Wall"))
{
return;
}
if (founds[i].gameObject.CompareTag("Fall"))
{
return;
}
thiTra.Translate(1, 0, 0);
}
}
else
{ // if there is an empty space
thiTra.Translate(1, 0, 0);
}
}
delay = maxDelay;
}
}
}
Can you clarify the sequence you're describing? For example, are you saying that you experience the following:
(time in seconds) - time 0: You start at 0.5/2.5/0 - time 1: You call Translate(0, -1, 0) and end up at 0.5/1.5/0 (desired) - time 2: You call Translate(0, -1, 0) and end up at 0.5/-6.5/0 (too far, expected 0.5/0.5/0) - etc...
Also, when you say "it happens whenever any corner hits 0,0,0", are you talking about a collider attached to the gameObject that you are translating? If so, are you using any physics with a rigidbody? Is there something it's colliding with?
I'm not using any physics at all, and there is literally nothing in the scene where it is colliding. I have a sprite backdrop but it is 5 back on the z and colliderless.
Outside of looking at the code involved, and given physics isn't in play or presumably anything else which might affect the position, I don't know what else to ask.
Can you post your code, or as reduced a version as you can? I understand you have program$$anonymous$$g knowledge but as I said, based on your description, you're right, it shouldn't be happening, so I would imagine the only way anyone can help at this point would be to take a look at what the code is doing.
Answer by EXZ_EXZ · Jun 05, 2018 at 01:46 PM
First of all there is no need for "thiTra" -> if the script is a component of the object you want to move you can just write transform.Translate instead of this.gameObject.transform.Translate :) To awnser your question: If I understood correctly you want to move an object every second to a specific place?! Take a look at: https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html This allows you to move an object to another position with a certain speed. Keep in mind that this has to take place in an update function. If you don't want movement and instead want to teleport the object every x seconds just use transform.position. This takes in a vector3. you could call this on a delay to teleport your object every x seconds
As I stated in my question, everything is working fine. I want it to teleport to the next location. But the PROBLE$$anonymous$$ is that it will teleport seven or eight units at once when a corner of the collider touches 0,0,0. There is no code that tells it to go seven or eight units downward, just one. And it always happens, no matter which corner of the collider touches 0,0,0.
Answer by lpye · Jun 05, 2018 at 03:58 PM
In each block where you loop through the collisions, if the colliding object is not a Wall or Fall, you move a step but continue looping. If you found yourself colliding with multiple non blocking collides it would cause you to translate multiple times.
Without knowing anything else about your scene that is where I would focus my attention.
But there is nothing there, and it only happens when it touches 0,0,0. Also it does it all at once, not paying attention to the delay. I am not sure how many times I have said this exact thing.
Your answer
![](https://koobas.hobune.stream/wayback/20220612164138im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Simple Teleportation Confusion 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Transform.Translate causing object to teleport 0 Answers
About "translating" js into C# 1 Answer