Thanks for the help guys!
I’ve been having an issue that actually caused me to try and use the reference to gameObject in the first place as an attempt to fix it.
I’m making a game where you there’s a board open box suspended in the air. The player tilts the box using the axis controls, an example of the box:
The board tilt maximum is currently set to 45.
The box tilts using this code:
using UnityEngine;
using System.Collections;
public class WorldController : MonoBehaviour {
public float tiltAngle;
public float smooth;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
//Get the input values.
float tiltX = Input.GetAxis ("Vertical") * tiltAngle;
float tiltZ = (Input.GetAxis ("Horizontal") * tiltAngle) * -1;
Quaternion target = Quaternion.Euler (tiltX, 0, tiltZ);
transform.rotation = Quaternion.Slerp (transform.rotation, target, Time.deltaTime * smooth);
}
}
I wanted to make obstacles that I can place in the world, which then move between two points in a single direction at a set speed.
My original scripts suceeded at this but I hit a problem. The obstacles would move outside of their set barriers when the gameboard tilted. All the obstacles were childs of the board so moved with it, but when they were at an angle, they would suddenly decide that instead of stopping where they usually would, they would instead shift further in one direction before returning.
My new script tried to tackle this by creating an empty gameObject on the starting position of the obstacle to store its location. This is because I thought that maybe the obstacle was moving along its axis as it rotated therefore limits set to its transform.position would be invalid.
The new script hasn’t fixed the problem, the changes seem less extreme now, but they are still there.
I created an object that moved forward and then back, the limit in each direction set to 2. If the object starting at z 10 it would move to z12 then back to z8 then repeat. This works fine when the board isn’t moving, however when it does it changes. For example it will start moving forward to z14 then back to z 10.
This is my full obstacle code, I’ve tried lots of different things and can’t figure out how to stop this shifting from happening. I’m hoping someone can help.
using UnityEngine;
using System.Collections;
public class ObstacleScript : MonoBehaviour {
private Transform gameBoard;
public GameObject midPointMarker;
private GameObject midPoint;
public string direction1;
public string direction2;
private Vector3 dir;
private Vector3 altDir;
public float direction1Limit;
public float direction2Limit;
private Vector3 maxDistance;
private Vector3 minDistance;
public float speed;
private Vector3 currentPos;
private bool onReturn = false;
// Use this for initialization
void Start () {
//Set dir and altDir to contain the translate.direction for movement.
SetDirections ();
//Instantiates a transform at the obstacles oirgin and sets a midPoint to hold the gameObject. The midPoint keeps aligned with the parent.
SetMidPoint ();
//Set maxDistance and minDistance relative from midPoint in the specified directions.
SetLimits ();
}
// Update is called once per frame
void Update () {
if (onReturn == false) {
if (direction1 == "forward") {
StartCoroutine ("MoveForward");
} else if (direction1 == "back") {
StartCoroutine ("MoveBack");
} else if (direction1 == "left") {
StartCoroutine ("MoveLeft");
} else if (direction1 == "right") {
StartCoroutine ("MoveRight");
} else if (direction1 == "down") {
StartCoroutine ("MoveDown");
} else if (direction1 == "up") {
StartCoroutine ("MoveUp");
}
}
if (onReturn == true) {
if (direction2 == "forward") {
StartCoroutine ("MoveForward");
} else if (direction2 == "back") {
StartCoroutine ("MoveBack");
} else if (direction2 == "left") {
StartCoroutine ("MoveLeft");
} else if (direction2 == "right") {
StartCoroutine ("MoveRight");
} else if (direction2 == "down") {
StartCoroutine ("MoveDown");
} else if (direction2 == "up") {
StartCoroutine ("MoveUp");
}
}
}
//Functions//
void SetDirections () {
if (direction1 == "forward") {
dir = Vector3.forward;
} else if (direction1 == "back") {
dir = Vector3.back;
} else if (direction1 == "up") {
dir = Vector3.up;
} else if (direction1 == "down") {
dir = Vector3.down;
} else if (direction1 == "left") {
dir = Vector3.left;
} else if (direction1 == "right") {
dir = Vector3.right;
}
if (direction2 == "forward") {
altDir = Vector3.forward;
} else if (direction2 == "back") {
altDir = Vector3.back;
} else if (direction2 == "up") {
altDir = Vector3.up;
} else if (direction2 == "down") {
altDir = Vector3.down;
} else if (direction2 == "left") {
altDir = Vector3.left;
} else if (direction2 == "right") {
altDir = Vector3.right;
}
}
void SetMidPoint () {
midPoint = (GameObject)Instantiate(midPointMarker, transform.position, transform.rotation);
gameBoard = transform.parent;
midPoint.transform.SetParent (gameBoard);
}
void SetLimits () {
if (direction1 == "forward") {
maxDistance.z = (midPoint.transform.position.z + direction1Limit);
} else if (direction1 == "back") {
maxDistance.z = (midPoint.transform.position.z - direction1Limit);
} else if (direction1 == "left") {
maxDistance.x = (midPoint.transform.position.x - direction1Limit);
} else if (direction1 == "right") {
maxDistance.x = (midPoint.transform.position.x + direction1Limit);
} else if (direction1 == "up") {
maxDistance.y = (midPoint.transform.position.y + direction1Limit);
} else if (direction1 == "down") {
maxDistance.y = (midPoint.transform.position.y - direction1Limit);
}
if (direction2 == "forward") {
minDistance.z = (midPoint.transform.position.z + direction2Limit);
} else if (direction2 == "back") {
minDistance.z = (midPoint.transform.position.z - direction2Limit);
} else if (direction2 == "left") {
minDistance.x = (midPoint.transform.position.x - direction2Limit);
} else if (direction2 == "right") {
minDistance.x = (midPoint.transform.position.x + direction2Limit);
} else if (direction2 == "up") {
minDistance.y = (midPoint.transform.position.y + direction2Limit);
} else if (direction2 == "down") {
minDistance.y = (midPoint.transform.position.y - direction2Limit);
}
}
//Coroutines//
IEnumerator MoveForward () {
//Determine whether or not to use dir or altDir.
if (direction1 == "forward") {
while ((transform.position.z) < maxDistance.z) {
transform.Translate (dir * (Time.deltaTime * speed));
maxDistance.z = midPoint.transform.position.z + direction1Limit;
yield return null;
}
} else {
while ((transform.position.z) < minDistance.z) {
transform.Translate (altDir * (Time.deltaTime * speed));
minDistance.z = midPoint.transform.position.z + direction2Limit;
yield return null;
}
}
//Detect whether or not this direction was its first or second move.
if (direction1 == "forward") {
onReturn = true;
} else {
onReturn = false;
}
}
IEnumerator MoveBack () {
//Determine whether or not to use dir or altDir.
if (direction1 == "back") {
while ((transform.position.z) > maxDistance.z) {
transform.Translate (dir * (Time.deltaTime * speed));
maxDistance.z = midPoint.transform.position.z - direction1Limit;
Debug.Log ("test1");
yield return null;
}
} else {
while ((transform.position.z) > minDistance.z) {
transform.Translate (altDir * (Time.deltaTime * speed));
minDistance.z = midPoint.transform.position.z - direction2Limit;
yield return null;
}
//Detect whether or not this direction was its first or second move.
}
if (direction1 == "back") {
onReturn = true;
} else {
onReturn = false;
}
}
IEnumerator MoveLeft () {
//Determine whether or not to use dir or altDir.
if (direction1 == "left") {
while ((transform.position.x) > maxDistance.x) {
transform.Translate (dir * (Time.deltaTime * speed));
maxDistance.x = midPoint.transform.position.x - direction1Limit;
yield return null;
}
} else {
while ((transform.position.x) > minDistance.x) {
transform.Translate (altDir * (Time.deltaTime * speed));
minDistance.x = midPoint.transform.position.x - direction2Limit;
yield return null;
}
}
//Detect whether or not this direction was its first or second move.
if (direction1 == "left") {
onReturn = true;
} else {
onReturn = false;
}
}
IEnumerator MoveRight () {
//Determine whether or not to use dir or altDir.
if (direction1 == "right") {
while ((transform.position.x) < maxDistance.x) {
transform.Translate (dir * (Time.deltaTime * speed));
maxDistance.x = midPoint.transform.position.x + direction1Limit;
yield return null;
}
} else {
while ((transform.position.x) < minDistance.x) {
transform.Translate (altDir * (Time.deltaTime * speed));
minDistance.x = midPoint.transform.position.x + direction2Limit;
yield return null;
}
}
//Detect whether or not this direction was its first or second move.
if (direction1 == "right") {
onReturn = true;
} else {
onReturn = false;
}
}
IEnumerator MoveDown() {
//Determine whether or not to use dir or altDir.
if (direction1 == "down") {
while ((transform.position.y) > maxDistance.y) {
transform.Translate (dir * (Time.deltaTime * speed));
maxDistance.y = midPoint.transform.position.y - direction1Limit;
yield return null;
}
} else {
while ((transform.position.y) > minDistance.y) {
transform.Translate (altDir * (Time.deltaTime * speed));
minDistance.y = midPoint.transform.position.y - direction2Limit;
yield return null;
}
}
//Detect whether or not this direction was its first or second move.
if (direction1 == "down") {
onReturn = true;
} else {
onReturn = false;
}
}
IEnumerator MoveUp () {
//Determine whether or not to use dir or altDir.
if (direction1 == "up") {
while ((transform.position.y) < maxDistance.y) {
transform.Translate (dir * (Time.deltaTime * speed));
maxDistance.y = midPoint.transform.position.y + direction1Limit;
yield return null;
}
} else {
while ((transform.position.y) < minDistance.y) {
transform.Translate (altDir * (Time.deltaTime * speed));
minDistance.y = midPoint.transform.position.y + direction2Limit;
yield return null;
}
}
//Detect whether or not this direction was its first or second move.
if (direction1 == "up") {
onReturn = true;
} else {
onReturn = false;
}
}
}