Solved, may be closed
These are the errors I get. My code within that range is the following:
RaycastHit ladderRayHit;
if (Physics.Raycast(position, Vector3.down, out ladderRayHit, 1f))
{
//Is the object a Ladder?
if (ladderRayHit.transform.tag == "Ladder")
{
//The object was a ladder
return true;
Debug.Log("The object bellow me was a ladder"); //Assets/Scripts/sPlayer.cs(280,47): warning CS0162: Unreachable code detected
}
else
{
//The object was not a ladder
return false;
Debug.Log("The object bellow me was NOT a ladder"); //Assets/Scripts/sPlayer.cs(286,47): warning CS0162: Unreachable code detected
}
}
else
{
return false;
Debug.Log("There was nothining bellow me"); //Assets/Scripts/sPlayer.cs(292,39): warning CS0162: Unreachable code detected
}
I placed comments in the code so you can see where the warnings points at.
I’ve been thinking about this for about 30 minutes, but just can’t figure out why the heck that is unreachable.
It seems like there is something wrong with the raycast, but I don’t see why :S
This peice of code is quite important for other things in my script to work, so I need to know why it’s unreachable.
Here’s the full script if you for some reason need to know it: (Warning: long)
using UnityEngine;
using System.Collections;
public class sPlayer : MonoBehaviour {
#region Variables
public GameObject _Ladder;
private float smoothTime = 0.15F;
private Vector3 velocity = Vector3.zero;
private float fallSpeed = 6f;
private Vector2 MiddleOfScreen;
private Vector3 myPosition;
private Rect rightButton, leftButton, upButton, downButton, restartButton, ladderButton;
#endregion
// Use this for initialization
void Start () {
#region Buttons
//Yupp, buttons
MiddleOfScreen = new Vector2(Screen.width / 2, Screen.height / 2);
rightButton = new Rect(MiddleOfScreen.x + 50, MiddleOfScreen.y - 50, 100, 100);
leftButton = new Rect(MiddleOfScreen.x - 150, MiddleOfScreen.y - 50, 100, 100);
upButton = new Rect(MiddleOfScreen.x - 50, MiddleOfScreen.y - 150, 100, 100);
downButton = new Rect(MiddleOfScreen.x - 50, MiddleOfScreen.y + 50, 100, 100);
restartButton = new Rect(20, 20, 60, 40);
ladderButton = new Rect(Screen.width - 120, Screen.height - 80, 100, 60);
#endregion
//Sets myPosition to starting position
myPosition = transform.position;
//Makes the player able to use the keyboard if running on correct device
if (Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.WindowsWebPlayer || Application.platform == RuntimePlatform.FlashPlayer)
{
gameObject.AddComponent("sPlayerKeyboard");
Debug.Log("Running on PC");
}
}
// Update is called once per frame
void Update () {
#region MoveLikeNormal
//Moves the player to it's position along the x and y axis
transform.position = Vector3.SmoothDamp(transform.position, new Vector3(myPosition.x, transform.position.y, myPosition.z), ref velocity, smoothTime);
if (isThereALadder("down", myPosition))
{
transform.position = Vector3.SmoothDamp(transform.position, new Vector3(transform.position.x, myPosition.y, transform.position.z), ref velocity, smoothTime);
}
#endregion
#region Gravity
//If the player should fall
if (myPosition.y < transform.position.y)
{
//If the player is not touching a ladder he should fall
if (!isThereALadder("down", myPosition) !isThereALadder("down", myPosition))
{
#region Fall
//make player fall
transform.Translate(Vector3.down * fallSpeed * Time.deltaTime, Space.World);
//If the player is almost as the correct position, snap him to it
if (Mathf.Round(myPosition.y * 10) == Mathf.Round(transform.position.y * 10))
{
transform.position = new Vector3(transform.position.x, myPosition.y, transform.position.z);
}
#endregion
}
}
//If the player fell to far
else if (isThereABlock("down", myPosition) myPosition.y > transform.position.y)
{
//correct the position
transform.position = new Vector3(transform.position.x, myPosition.y, transform.position.z);
}
#endregion
}
void FixedUpdate()
{
//Makes the player fall if there is no block under him.
if (!isThereABlock("down", myPosition)
!isThereALadder("middle", myPosition)
!isThereALadder("down", myPosition))
{
myPosition += Vector3.down;
Debug.Log("DER IS NO LADDER BELLOW MEH");
}
}
//Makes the player move
public void Move(string direction)
{
if (direction == "down")
{
if (isThereABlock(direction, myPosition))
{
RaycastHit moveRayHit;
if (Physics.Raycast(myPosition, Vector3.down, out moveRayHit, 1))
{
moveRayHit.transform.GetComponent<sBlock>().Destroyed();
}
}
}
else if (direction == "left")
{
if (isThereABlock(direction, myPosition))
{
RaycastHit moveRayHit;
if (Physics.Raycast(myPosition, Vector3.left, out moveRayHit, 1))
{
moveRayHit.transform.GetComponent<sBlock>().Destroyed();
}
}
myPosition.x -= 1;
}
else if (direction == "right")
{
if (isThereABlock(direction, myPosition))
{
RaycastHit moveRayHit;
if (Physics.Raycast(myPosition, Vector3.right, out moveRayHit, 1))
{
moveRayHit.transform.GetComponent<sBlock>().Destroyed();
}
}
myPosition.x += 1;
}
else if (direction == "up")
{
if (isThereABlock(direction, myPosition))
{
Debug.Log("Block above");
RaycastHit moveRayHit;
if (Physics.Raycast(myPosition, Vector3.up, out moveRayHit, 1))
{
moveRayHit.transform.GetComponent<sBlock>().Destroyed();
}
}
if (isThereALadder("middle", myPosition))
{
Debug.Log("Can go up");
myPosition.y += 1;
}
}
}
public bool isThereABlock(string direction, Vector3 position)
{
RaycastHit isThereABlockRayHit;
if (direction == "down")
{
if (Physics.Raycast(position, Vector3.down, out isThereABlockRayHit, 1))
{
if (isThereABlockRayHit.transform.tag == "Block")
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else if (direction == "left")
{
if (Physics.Raycast(position, Vector3.left, out isThereABlockRayHit, 1))
{
if (isThereABlockRayHit.transform.tag == "Block")
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else if (direction == "right")
{
if (Physics.Raycast(position, Vector3.right, out isThereABlockRayHit, 1))
{
if (isThereABlockRayHit.transform.tag == "Block")
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else if (direction == "up")
{
if (Physics.Raycast(position, Vector3.up, out isThereABlockRayHit, 1))
{
if (isThereABlockRayHit.transform.tag == "Block")
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
Debug.LogError(direction + " is not a valid string for isThereABlock!");
return false;
}
}
public bool isThereALadder(string direction, Vector3 position)
{
if (direction == "middle")
{
#region Middle
RaycastHit ladderRayHit;
position = new Vector3(position.x, position.y, position.z + 1); //Replace position so that the Raycast will work
if (Physics.Raycast(position, Vector3.back, out ladderRayHit, 1f))
{
//Is the object a Ladder?
if (ladderRayHit.transform.tag == "Ladder")
{
//The object was a ladder
return true;
}
else
{
//The object was not a ladder
return false;
}
}
else
{
return false;
}
#endregion
}
else if (direction == "down")
{
#region Down
RaycastHit ladderRayHit;
if (Physics.Raycast(position, Vector3.down, out ladderRayHit, 1f))
{
//Is the object a Ladder?
if (ladderRayHit.transform.tag == "Ladder")
{
//The object was a ladder
return true;
Debug.Log("The object bellow me was a ladder");
}
else
{
//The object was not a ladder
return false;
Debug.Log("The object bellow me was NOT a ladder");
}
}
else
{
return false;
Debug.Log("Der was nothining bellow me");
}
#endregion
}
else
{
#region Error
Debug.LogError(direction + " is not a valid string for isThereALadder!");
return false;
#endregion
}
}
void OnGUI()
{
if (GUI.Button(rightButton, "", GUIStyle.none))
{
Move("right");
}
if (GUI.Button(leftButton, "", GUIStyle.none))
{
Move("left");
}
if (GUI.Button(upButton, "", GUIStyle.none))
{
Move("up");
}
if (GUI.Button(downButton, "", GUIStyle.none))
{
Move("down");
}
if (GUI.Button(restartButton, "Restart"))
{
Application.LoadLevel(Application.loadedLevel);
}
if (GUI.Button(ladderButton, "Ladder"))
{
Instantiate(_Ladder,
new Vector3(myPosition.x, Mathf.Round(myPosition.y), myPosition.z + .5f), //Places the Ladder in the correct position
Quaternion.Euler(new Vector3(-90, 0, 0)));
}
}
void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
Gizmos.DrawSphere(myPosition, .2f);
}
}
Any help would be appreciated ![]()
Also, sorry if I make any obvious mistakes. I’m quite new to C# but I’m starting to get the hang of it ![]()