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*.gameObject.CompareTag("Wall"))*
{
return;
}
if (founds*.gameObject.CompareTag(“Fall”))*
{
return;
}
thiTra.Translate(0, 1, 0);
}
}
else
{ // if there is an empty space
thiTra.Translate(0, 1, 0);
}
}
if (GravCont.GetComponent().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*.gameObject.CompareTag(“Wall”))*
{
return;
}
if (founds*.gameObject.CompareTag(“Fall”))*
{
return;
}
thiTra.Translate(0, -1, 0);
}
}
else
{ // if there is an empty space
thiTra.Translate(0, -1, 0);
}
}
if (GravCont.GetComponent().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*.gameObject.CompareTag(“Wall”))*
{
return;
}
if (founds*.gameObject.CompareTag(“Fall”))*
{
return;
}
thiTra.Translate(-1, 0, 0);
}
}
else
{ // if there is an empty space
thiTra.Translate(-1, 0, 0);
}
}
if (GravCont.GetComponent().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*.gameObject.CompareTag(“Wall”))*
{
return;
}
if (founds*.gameObject.CompareTag(“Fall”))*
{
return;
}
thiTra.Translate(1, 0, 0);
}
}
else
{ // if there is an empty space
thiTra.Translate(1, 0, 0);
}
}
delay = maxDelay;
}
* }*
}