Hi everyone!
My problem is my movement script. I’m not trying to have my character move around freely or normally, I want the player (and all other objects) to move around from point to point like hoping across tiles.
The issue I have with this is that because my character will just appear ( or hopefully one day Lerp) to the point i want to move to, I fear I’ll have a collision issue if there is say a wall in that spot and my player will just continue to appear within the wall and ignore the collision because the translation is set.
So I need to know how to see if a specific position has an object in it already and possibly what that object is.
Very grateful Thanks!
johne5
February 14, 2016, 1:38pm
2
I did something like this once.
My player object moved 1 unit square at a time. before it moved it checked what was at the destination.
Because it was only 1 square at a time I shot a raycast in the direction it was going to move. read the tag of any object that was fit. IE: walls, power ups, bad guys. I’ll post my move script. let me know if you need any help. there is a lot in here about color changing and shape changing, you will not need any of that.
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour {
public static int CurrentLevelName;
public float lerpDuration = 5;
static public float wRotation = 180.0F;
static public float sRotation = 180.0F;
static public float aRotation = 90.0F;
static public float dRotation = 270.0F;
public Mesh sphere;
public Mesh cube;
public Mesh triangle;
public static Transform ObjectBelow;
private RaycastHit hitinfo;
private MeshFilter meshFilter;
public static Transform futureObjectBelow;
private Transform futureColorBelow;
public RaycastHit futurehit;
private Vector3 endPOS;
//private Vector3 rayCastFuture;
private bool idoLerp = false;
public string Shape2;
public string Color1;
public string Color2;
public string Shape1;
public string LastColorTag;
public string LastShapeTag;
//public Texture2D ShapeResult;
public Texture2D circleShape;
public Texture2D triShape;
public Texture2D squareShape;
public Vector3 StartPOS;
public int LevelNumber = 0;
public static int Par = 10;
public int minPar = 0;
private Texture resultGeorge;
//bool firstShapeStored;
bool secondShape;
//bool firstColorStored;
private bool OnBridge;
public static int movesMade = 0;
public AudioClip moveSound;
public AudioClip backwardsSound;
void Start () {
Par = minPar;
CurrentLevelName = Application.loadedLevel;
StartPOS = (gameObject.transform.position);
reset();
}
void Update ()
{
if (!idoLerp) {
transform.position = new Vector3 (Mathf.RoundToInt (gameObject.transform.position.x), gameObject.transform.position.y, Mathf.RoundToInt (gameObject.transform.position.z));
if (Input.GetKeyDown ("w")) {
audio.Stop();
idoLerp = GetColor (Vector3.forward);
GetFutureObject (Vector3.forward * 2);
endPOS = new Vector3 (gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z + 2);
transform.eulerAngles = new Vector3 (270, wRotation, 0);
}
else if (Input.GetKeyDown ("s")) {
audio.Stop();
idoLerp = GetColor (Vector3.forward * -1);
GetFutureObject (Vector3.forward * -2);
endPOS = new Vector3 (gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z + -2);
transform.eulerAngles = new Vector3 (270, sRotation, 0);
}
else if (Input.GetKeyDown ("a")) {
audio.Stop();
idoLerp = GetColor (Vector3.right * -1);
GetFutureObject (Vector3.right * -2);
endPOS = new Vector3 (gameObject.transform.position.x + -2, gameObject.transform.position.y, gameObject.transform.position.z);
transform.eulerAngles = new Vector3 (270, aRotation, 0);
}
else if (Input.GetKeyDown ("d")) {
audio.Stop();
idoLerp = GetColor (Vector3.right);
GetFutureObject (Vector3.right * 2);
endPOS = new Vector3 (gameObject.transform.position.x + 2, gameObject.transform.position.y, gameObject.transform.position.z);
transform.eulerAngles = new Vector3 (270, dRotation, 0);
}
} else if (idoLerp) {
if (futureColorBelow.gameObject.tag == "onetime" && futureColorBelow != null)
{
futureColorBelow.gameObject.SetActiveRecursively (false);
}
audio.PlayOneShot (moveSound);
transform.position = Vector3.Lerp (gameObject.transform.position, endPOS, lerpDuration * Time.deltaTime);
if ((gameObject.transform.position.z + 0.1) >= endPOS.z && (gameObject.transform.position.z + -0.1) <= endPOS.z && (gameObject.transform.position.x + 0.1) >= endPOS.x && (gameObject.transform.position.x + -0.1) <= endPOS.x)
{
idoLerp = false;
movesMade++;
}
}
if (!idoLerp) {
if (Physics.Raycast (transform.position, Vector3.down, out hitinfo, 5, 1 << 0)) {
ObjectBelow = hitinfo.transform.parent;
meshFilter = gameObject.GetComponent<MeshFilter> ();
}
//print(Application.loadedLevel.ToString());
if (Application.loadedLevel.ToString () == "1")
{
if (ObjectBelow.tag == "finish" && Shape2 == "triangle" && Color2 == "green")
Application.LoadLevel ("LevelComplete");
}
else if (Application.loadedLevel.ToString () == "2")
{
if (ObjectBelow.tag == "finish" && Shape2 == "circle" && Color2 == "green")
Application.LoadLevel ("LevelComplete");
}
else if (Application.loadedLevel.ToString () == "3")
{
if (ObjectBelow.tag == "finish" && Shape2 == "square" && Color2 == "red")
Application.LoadLevel ("LevelComplete");
}
else if (Application.loadedLevel.ToString () == "4")
{
if (ObjectBelow.tag == "finish" && Shape2 == "square" && Color2 == "red")
Application.LoadLevel ("LevelComplete");
}
else if (Application.loadedLevel.ToString () == "5")
{
if (ObjectBelow.tag == "finish" && Shape2 == "triangle" && Color2 == "blue")
{
Application.LoadLevel ("LevelComplete");
}
}
else if (Application.loadedLevel.ToString () == "6")
{
if (ObjectBelow.tag == "finish" && Shape2 == "triangle" && Color2 == "blue")
Application.LoadLevel ("LevelComplete");
}
else if (Application.loadedLevel.ToString () == "7")
{
if (ObjectBelow.tag == "finish" && Shape2 == "square" && Color2 == "blue")
Application.LoadLevel ("LevelComplete");
}
else if (Application.loadedLevel.ToString () == "8")
{
if (ObjectBelow.tag == "finish" && Shape2 == "triangle" && Color2 == "blue")
Application.LoadLevel ("LevelComplete");
}
else if (Application.loadedLevel.ToString () == "9")
{
if (ObjectBelow.tag == "finish" && Shape2 == "square" && Color2 == "red")
Application.LoadLevel ("LevelComplete");
}
else if (Application.loadedLevel.ToString () == "10")
{
if (ObjectBelow.tag == "finish" && Shape2 == "circle" && Color2 == "green")
Application.LoadLevel ("LevelComplete"); //Make win screen
}
}
}//end update()
void CheckColorTag()
{
if(Color2 == "red")
{
if(Color1 == "blue")
{
renderer.material.color = Color.green;
Color2 = "green";
}
else if(Color1 == "green")
{
renderer.material.color = Color.blue;
Color2 = "blue";
}
}
else if(Color2 == "green")
{
if(Color1 == "red")
{
renderer.material.color = Color.blue;
Color2 = "blue";
}
else if(Color1 == "blue")
{
renderer.material.color = Color.red;
Color2 = "red";
}
}
else if(Color2 == "blue")
{
if(Color1 == "red")
{
renderer.material.color = Color.green;
Color2 = "green";
}
else if(Color1 == "green")
{
renderer.material.color = Color.red;
Color2 = "red";
}
}
else
{
print ("It dun Color goofed!");
print ("Color1 last bridge color = " + Color1);
print ("Color2 current coolor = " + Color2);
}
}
void CheckShapeTag()
{
if(futureColorBelow != null)
{
if(Shape2 == "circle")
{
if(Shape1 == "square")
{
meshFilter.mesh = triangle;
Shape2 = "triangle";
}
else if(Shape1 == "triangle")
{
meshFilter.mesh = cube;
Shape2 = "square";
}
}
else if(Shape2 == "square")
{
if(Shape1 == "circle")
{
meshFilter.mesh = triangle;
Shape2 = "triangle";
}
else if(Shape1 == "triangle")
{
meshFilter.mesh = sphere;
Shape2 = "circle";
}
}
else if(Shape2 == "triangle")
{
if(Shape1 == "circle")
{
meshFilter.mesh = cube;
Shape2 = "square";
}
else if(Shape1 == "square")
{
meshFilter.mesh = sphere;
Shape2 = "circle";
}
}
else
{
print ("It dun Shape goofed!");
print ("Shape1 last shape = " + Shape1);
print ("Shape2 current shape = " + Shape2);
}
}
}
bool StoreShapes(){
bool result = false;
if(futureObjectBelow != null)
{
if(LastShapeTag != null && futureObjectBelow != null)
{
if(futureObjectBelow.gameObject.tag != "blank" && futureObjectBelow.gameObject.tag != "onetime")
{
Shape1 = futureObjectBelow.gameObject.tag;
CheckShapeTag();
}
}
LastShapeTag = futureObjectBelow.gameObject.tag;
}
return result;
}
bool StoreColors(){
bool result = false;
if(futureColorBelow != null)
{
if(LastColorTag != null && futureColorBelow != null)
{
if(futureColorBelow.gameObject.tag != "blank" && futureColorBelow.gameObject.tag != "onetime")
{
Color1 = futureColorBelow.gameObject.tag;
CheckColorTag();
}
}
LastColorTag = futureColorBelow.gameObject.tag;
}
return result;
}
bool GetColor(Vector3 DirectionMod)
{
bool result = false;
RaycastHit FutureColor;
Vector3 ColorRayCast = new Vector3(transform.position.x + DirectionMod.x, transform.position.y + DirectionMod.y, transform.position.z + DirectionMod.z);
futureColorBelow = null;
if(Physics.Raycast(ColorRayCast, Vector3.down, out FutureColor, 5, 1 << 0))
{
futureColorBelow = FutureColor.transform.parent;
result = true;
if(futureColorBelow != null)
{
StoreColors();
}
}
else
result = false;
return result;
}
bool GetFutureObject(Vector3 DirectionMod)
{
bool result = false;
RaycastHit FutureObject;
Vector3 FutureObjPos = new Vector3(transform.position.x + DirectionMod.x, transform.position.y + DirectionMod.y, transform.position.z + DirectionMod.z);
if(Physics.Raycast(FutureObjPos, Vector3.down, out FutureObject, 5, 1 << 0))
{
futureObjectBelow = FutureObject.transform.parent;
result = true;
if(futureObjectBelow != null)
{
StoreShapes();
}
}
else
result = false;
return result;
}
public static void reset(){
GameObject Player = GameObject.Find ("MrCube");
PlayerMove PlayerMoveScript = Player.GetComponent<PlayerMove>();
PlayerMoveScript.Shape2 = "circle";
PlayerMoveScript.LastShapeTag = "circle";
PlayerMoveScript.Color2 = "green";
PlayerMoveScript.LastColorTag = "green";
Player.renderer.material.color = Color.green; //start as green
PlayerMoveScript.meshFilter = Player.gameObject.GetComponent<MeshFilter>(); //start as a sphere/circle
Mesh gMesh = PlayerMoveScript.sphere;
PlayerMoveScript.meshFilter.mesh = gMesh; //start as a sphere/circle
movesMade = 0;
PlayerMoveScript.idoLerp = false;
}
}
johne5:
I did something like this once.
My player object moved 1 unit square at a time. before it moved it checked what was at the destination.
Because it was only 1 square at a time I shot a raycast in the direction it was going to move. read the tag of any object that was fit. IE: walls, power ups, bad guys. I’ll post my move script. let me know if you need any help. there is a lot in here about color changing and shape changing, you will not need any of that.
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour {
public static int CurrentLevelName;
public float lerpDuration = 5;
static public float wRotation = 180.0F;
static public float sRotation = 180.0F;
static public float aRotation = 90.0F;
static public float dRotation = 270.0F;
public Mesh sphere;
public Mesh cube;
public Mesh triangle;
public static Transform ObjectBelow;
private RaycastHit hitinfo;
private MeshFilter meshFilter;
public static Transform futureObjectBelow;
private Transform futureColorBelow;
public RaycastHit futurehit;
private Vector3 endPOS;
//private Vector3 rayCastFuture;
private bool idoLerp = false;
public string Shape2;
public string Color1;
public string Color2;
public string Shape1;
public string LastColorTag;
public string LastShapeTag;
//public Texture2D ShapeResult;
public Texture2D circleShape;
public Texture2D triShape;
public Texture2D squareShape;
public Vector3 StartPOS;
public int LevelNumber = 0;
public static int Par = 10;
public int minPar = 0;
private Texture resultGeorge;
//bool firstShapeStored;
bool secondShape;
//bool firstColorStored;
private bool OnBridge;
public static int movesMade = 0;
public AudioClip moveSound;
public AudioClip backwardsSound;
void Start () {
Par = minPar;
CurrentLevelName = Application.loadedLevel;
StartPOS = (gameObject.transform.position);
reset();
}
void Update ()
{
if (!idoLerp) {
transform.position = new Vector3 (Mathf.RoundToInt (gameObject.transform.position.x), gameObject.transform.position.y, Mathf.RoundToInt (gameObject.transform.position.z));
if (Input.GetKeyDown ("w")) {
audio.Stop();
idoLerp = GetColor (Vector3.forward);
GetFutureObject (Vector3.forward * 2);
endPOS = new Vector3 (gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z + 2);
transform.eulerAngles = new Vector3 (270, wRotation, 0);
}
else if (Input.GetKeyDown ("s")) {
audio.Stop();
idoLerp = GetColor (Vector3.forward * -1);
GetFutureObject (Vector3.forward * -2);
endPOS = new Vector3 (gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z + -2);
transform.eulerAngles = new Vector3 (270, sRotation, 0);
}
else if (Input.GetKeyDown ("a")) {
audio.Stop();
idoLerp = GetColor (Vector3.right * -1);
GetFutureObject (Vector3.right * -2);
endPOS = new Vector3 (gameObject.transform.position.x + -2, gameObject.transform.position.y, gameObject.transform.position.z);
transform.eulerAngles = new Vector3 (270, aRotation, 0);
}
else if (Input.GetKeyDown ("d")) {
audio.Stop();
idoLerp = GetColor (Vector3.right);
GetFutureObject (Vector3.right * 2);
endPOS = new Vector3 (gameObject.transform.position.x + 2, gameObject.transform.position.y, gameObject.transform.position.z);
transform.eulerAngles = new Vector3 (270, dRotation, 0);
}
} else if (idoLerp) {
if (futureColorBelow.gameObject.tag == "onetime" && futureColorBelow != null)
{
futureColorBelow.gameObject.SetActiveRecursively (false);
}
audio.PlayOneShot (moveSound);
transform.position = Vector3.Lerp (gameObject.transform.position, endPOS, lerpDuration * Time.deltaTime);
if ((gameObject.transform.position.z + 0.1) >= endPOS.z && (gameObject.transform.position.z + -0.1) <= endPOS.z && (gameObject.transform.position.x + 0.1) >= endPOS.x && (gameObject.transform.position.x + -0.1) <= endPOS.x)
{
idoLerp = false;
movesMade++;
}
}
if (!idoLerp) {
if (Physics.Raycast (transform.position, Vector3.down, out hitinfo, 5, 1 << 0)) {
ObjectBelow = hitinfo.transform.parent;
meshFilter = gameObject.GetComponent<MeshFilter> ();
}
//print(Application.loadedLevel.ToString());
if (Application.loadedLevel.ToString () == "1")
{
if (ObjectBelow.tag == "finish" && Shape2 == "triangle" && Color2 == "green")
Application.LoadLevel ("LevelComplete");
}
else if (Application.loadedLevel.ToString () == "2")
{
if (ObjectBelow.tag == "finish" && Shape2 == "circle" && Color2 == "green")
Application.LoadLevel ("LevelComplete");
}
else if (Application.loadedLevel.ToString () == "3")
{
if (ObjectBelow.tag == "finish" && Shape2 == "square" && Color2 == "red")
Application.LoadLevel ("LevelComplete");
}
else if (Application.loadedLevel.ToString () == "4")
{
if (ObjectBelow.tag == "finish" && Shape2 == "square" && Color2 == "red")
Application.LoadLevel ("LevelComplete");
}
else if (Application.loadedLevel.ToString () == "5")
{
if (ObjectBelow.tag == "finish" && Shape2 == "triangle" && Color2 == "blue")
{
Application.LoadLevel ("LevelComplete");
}
}
else if (Application.loadedLevel.ToString () == "6")
{
if (ObjectBelow.tag == "finish" && Shape2 == "triangle" && Color2 == "blue")
Application.LoadLevel ("LevelComplete");
}
else if (Application.loadedLevel.ToString () == "7")
{
if (ObjectBelow.tag == "finish" && Shape2 == "square" && Color2 == "blue")
Application.LoadLevel ("LevelComplete");
}
else if (Application.loadedLevel.ToString () == "8")
{
if (ObjectBelow.tag == "finish" && Shape2 == "triangle" && Color2 == "blue")
Application.LoadLevel ("LevelComplete");
}
else if (Application.loadedLevel.ToString () == "9")
{
if (ObjectBelow.tag == "finish" && Shape2 == "square" && Color2 == "red")
Application.LoadLevel ("LevelComplete");
}
else if (Application.loadedLevel.ToString () == "10")
{
if (ObjectBelow.tag == "finish" && Shape2 == "circle" && Color2 == "green")
Application.LoadLevel ("LevelComplete"); //Make win screen
}
}
}//end update()
void CheckColorTag()
{
if(Color2 == "red")
{
if(Color1 == "blue")
{
renderer.material.color = Color.green;
Color2 = "green";
}
else if(Color1 == "green")
{
renderer.material.color = Color.blue;
Color2 = "blue";
}
}
else if(Color2 == "green")
{
if(Color1 == "red")
{
renderer.material.color = Color.blue;
Color2 = "blue";
}
else if(Color1 == "blue")
{
renderer.material.color = Color.red;
Color2 = "red";
}
}
else if(Color2 == "blue")
{
if(Color1 == "red")
{
renderer.material.color = Color.green;
Color2 = "green";
}
else if(Color1 == "green")
{
renderer.material.color = Color.red;
Color2 = "red";
}
}
else
{
print ("It dun Color goofed!");
print ("Color1 last bridge color = " + Color1);
print ("Color2 current coolor = " + Color2);
}
}
void CheckShapeTag()
{
if(futureColorBelow != null)
{
if(Shape2 == "circle")
{
if(Shape1 == "square")
{
meshFilter.mesh = triangle;
Shape2 = "triangle";
}
else if(Shape1 == "triangle")
{
meshFilter.mesh = cube;
Shape2 = "square";
}
}
else if(Shape2 == "square")
{
if(Shape1 == "circle")
{
meshFilter.mesh = triangle;
Shape2 = "triangle";
}
else if(Shape1 == "triangle")
{
meshFilter.mesh = sphere;
Shape2 = "circle";
}
}
else if(Shape2 == "triangle")
{
if(Shape1 == "circle")
{
meshFilter.mesh = cube;
Shape2 = "square";
}
else if(Shape1 == "square")
{
meshFilter.mesh = sphere;
Shape2 = "circle";
}
}
else
{
print ("It dun Shape goofed!");
print ("Shape1 last shape = " + Shape1);
print ("Shape2 current shape = " + Shape2);
}
}
}
bool StoreShapes(){
bool result = false;
if(futureObjectBelow != null)
{
if(LastShapeTag != null && futureObjectBelow != null)
{
if(futureObjectBelow.gameObject.tag != "blank" && futureObjectBelow.gameObject.tag != "onetime")
{
Shape1 = futureObjectBelow.gameObject.tag;
CheckShapeTag();
}
}
LastShapeTag = futureObjectBelow.gameObject.tag;
}
return result;
}
bool StoreColors(){
bool result = false;
if(futureColorBelow != null)
{
if(LastColorTag != null && futureColorBelow != null)
{
if(futureColorBelow.gameObject.tag != "blank" && futureColorBelow.gameObject.tag != "onetime")
{
Color1 = futureColorBelow.gameObject.tag;
CheckColorTag();
}
}
LastColorTag = futureColorBelow.gameObject.tag;
}
return result;
}
bool GetColor(Vector3 DirectionMod)
{
bool result = false;
RaycastHit FutureColor;
Vector3 ColorRayCast = new Vector3(transform.position.x + DirectionMod.x, transform.position.y + DirectionMod.y, transform.position.z + DirectionMod.z);
futureColorBelow = null;
if(Physics.Raycast(ColorRayCast, Vector3.down, out FutureColor, 5, 1 << 0))
{
futureColorBelow = FutureColor.transform.parent;
result = true;
if(futureColorBelow != null)
{
StoreColors();
}
}
else
result = false;
return result;
}
bool GetFutureObject(Vector3 DirectionMod)
{
bool result = false;
RaycastHit FutureObject;
Vector3 FutureObjPos = new Vector3(transform.position.x + DirectionMod.x, transform.position.y + DirectionMod.y, transform.position.z + DirectionMod.z);
if(Physics.Raycast(FutureObjPos, Vector3.down, out FutureObject, 5, 1 << 0))
{
futureObjectBelow = FutureObject.transform.parent;
result = true;
if(futureObjectBelow != null)
{
StoreShapes();
}
}
else
result = false;
return result;
}
public static void reset(){
GameObject Player = GameObject.Find ("MrCube");
PlayerMove PlayerMoveScript = Player.GetComponent<PlayerMove>();
PlayerMoveScript.Shape2 = "circle";
PlayerMoveScript.LastShapeTag = "circle";
PlayerMoveScript.Color2 = "green";
PlayerMoveScript.LastColorTag = "green";
Player.renderer.material.color = Color.green; //start as green
PlayerMoveScript.meshFilter = Player.gameObject.GetComponent<MeshFilter>(); //start as a sphere/circle
Mesh gMesh = PlayerMoveScript.sphere;
PlayerMoveScript.meshFilter.mesh = gMesh; //start as a sphere/circle
movesMade = 0;
PlayerMoveScript.idoLerp = false;
}
}
Thanks a bunch Im doing the same idea with the movement greatly appreciated!