- Home /
Physics.Raycast.hit not working properly with moving chars...
So, i am making kind of a roguelike game with speed/turn based movement, and i use raycast.hit for every char to check if there is something at the position the specific char wants to move to. And if there is, then do not move there.
Gave a random movement for npcs, and if they bump into wall, other npc, everything works.
But sometimes when npc decides to move at the same spot i picked, he does it, and therfor we end at the same position. (Doesnt always happen. Sometimes he bumps into me, and sometimes not.)
It may be something with us both moving at the same moment, but it only happens with player char <-> npc char. Heres the player movement script, npc movement script, and the game manager script. I've only started scripting few days ago, so nevermind beginner mistakes. Any idea ?
public var speed : int;
function Update ()
{
if (Input.GetKeyDown (KeyCode.Keypad5))
{
Move(0,0);
}
if (Input.GetKeyDown (KeyCode.Keypad8))
{
var hit : RaycastHit;
if (Physics.Raycast (transform.position, Vector3(0, 0, 1), hit, 1))
{
if (hit.collider.tag == "Wall")
{
Debug.Log("Walnelismy w sciane z góry");
}
else if (hit.collider.tag == "Enemy")
{
Debug.Log("we bumped into some enemy.. would of attack him if it wasn't a prealpha version of the game!");
}
}
else
{
Move(0,1.4);
}
}
if (Input.GetKeyDown (KeyCode.Keypad6))
{
if (Physics.Raycast (transform.position, Vector3(1, 0, 0), hit, 1))
{
if (hit.collider.tag == "Wall")
{
Debug.Log("Walnelismy w sciane z prawej");
}
else if (hit.collider.tag == "Enemy")
{
Debug.Log("we bumped into some enemy.. would of attack him if it wasn't a prealpha version of the game!");
}
}
else
{
Move(1,0);
}
}
if (Input.GetKeyDown (KeyCode.Keypad2))
{
if (Physics.Raycast (transform.position, Vector3(0, 0, -1), hit, 1))
{
if (hit.collider.tag == "Wall")
{
Debug.Log("Walnelismy w sciane z dołu");
}
else if (hit.collider.tag == "Enemy")
{
Debug.Log("we bumped into some enemy.. would of attack him if it wasn't a prealpha version of the game!");
}
}
else
{
Move(0,-1.4);
}
}
if (Input.GetKeyDown (KeyCode.Keypad4))
{
if (Physics.Raycast (transform.position, Vector3(-1, 0, 0), hit, 1))
{
if (hit.collider.tag == "Wall")
{
Debug.Log("Walnelismy w sciane z lewej");
}
else if (hit.collider.tag == "Enemy")
{
Debug.Log("we bumped into some enemy.. would of attack him if it wasn't a prealpha version of the game!");
}
}
else
{
Move(-1,0);
}
}
if (Input.GetKeyDown (KeyCode.Keypad9))
{
if (Physics.Raycast (transform.position, Vector3(1, 0, 1.4), hit, 1))
{
if (hit.collider.tag == "Wall")
{
Debug.Log("Walnelismy w sciane z góra-prawo");
}
else if (hit.collider.tag == "Enemy")
{
Debug.Log("we bumped into some enemy.. would of attack him if it wasn't a prealpha version of the game!");
}
}
else
{
Move(1,1.4);
}
}
if (Input.GetKeyDown (KeyCode.Keypad3))
{
if (Physics.Raycast (transform.position, Vector3(1, 0, -1.4), hit, 1))
{
if (hit.collider.tag == "Wall")
{
Debug.Log("Walnelismy w sciane z dół-prawo");
}
else if (hit.collider.tag == "Enemy")
{
Debug.Log("we bumped into some enemy.. would of attack him if it wasn't a prealpha version of the game!");
}
}
else
{
Move(1,-1.4);
}
}
if (Input.GetKeyDown (KeyCode.Keypad1))
{
if (Physics.Raycast (transform.position, Vector3(-1, 0, -1.4), hit, 1))
{
if (hit.collider.tag == "Wall")
{
Debug.Log("Walnelismy w sciane z dół-lewo");
}
else if (hit.collider.tag == "Enemy")
{
Debug.Log("we bumped into some enemy.. would of attack him if it wasn't a prealpha version of the game!");
}
}
else
{
Move(-1,-1.4);
}
}
if (Input.GetKeyDown (KeyCode.Keypad7))
{
if (Physics.Raycast (transform.position, Vector3(-1, 0, 1.4), hit, 1))
{
if (hit.collider.tag == "Wall")
{
Debug.Log("Walnelismy w sciane z góra-lewo");
}
else if (hit.collider.tag == "Enemy")
{
Debug.Log("we bumped into some enemy.. would of attack him if it wasn't a prealpha version of the game!");
}
}
else
{
Move(-1,1.4);
}
}
}
function Move (X : float, Z : float)
{
rigidbody.position += Vector3(X,0,Z);
transform.position = rigidbody.position;
GameManager.instance.turn = GameManager.instance.turn + speed;
GameManager.instance.speed = speed;
}
public var turnspassed : int;
public var turnsrequired : int;
function Update () {
turnspassed += GameManager.instance.speed;
if (turnspassed >= turnsrequired)
{
var tempselectedx = Random.Range(0, 3);
var tempselectedz = Random.Range(0, 3);
var selectedx : float = 0;
var selectedz : float = 0;
if (tempselectedx == 0)
{
selectedx = 0;
}
else if (tempselectedx == 1)
{
selectedx = 1;
}
else if (tempselectedx == 2)
{
selectedx = -1;
}
if (tempselectedz == 0)
{
selectedz = 0;
}
else if (tempselectedz == 1)
{
selectedz = 1.4;
}
else if (tempselectedz == 2)
{
selectedz = -1.4;
}
turnspassed -= turnsrequired;
Move(selectedx, selectedz);
}
}
function Move (X : float, Z : float)
{
var hit : RaycastHit;
if (Physics.Raycast (transform.position, Vector3(X, 0, Z), hit, 1))
{
if (hit.collider.tag == "Wall")
{
Debug.Log("MOB has crashed into the wall");
turnspassed = turnspassed + turnsrequired;
}
else if (hit.collider.tag == "Playerchar")
{
Debug.Log("MOB has bumped into the player's char.. would of kick his ass, but wont yet.");
}
else if (hit.collider.tag == "Enemy")
{
Debug.Log("BOING, two enemies hit each other..");
turnspassed = turnspassed + turnsrequired;
}
}
else
{
rigidbody.position += Vector3(X,0,Z);
transform.position = rigidbody.position;
}
}
public var turn : int;
public var speed : int;
static var instance : GameManager = null;
function Awake()
{
if (!instance)
{
instance = this;
}
}
function LateUpdate()
{
speed = 0;
}
Your answer
Follow this Question
Related Questions
Custom Collision Detection 4 Answers
Can't detect collision. 1 Answer
detecting reycast collisions 1 Answer
Issues with Bullet Ricochet 1 Answer
Teleporting To Back Of Object? 1 Answer