- Home /
Physics raycast hit offset from where it should be
as seen in the pic I have a collider attached to a object, when I attempt to walk up to the object and raycast on it I can see the ray intersect the collider but it wont regester a hit, moving around I found out that it thinks the object to accually be up and to the right of it posI made a small GUI element to show the direction the player is facing and the range of the raycast(as ive not got to animations yet) I also added a gui box to show when the player is infront of and facing the object, I'm very confused as to why it thinks the object is over to the right and up farther then it is is stumping me
heres my raycast C# script
Debug.DrawLine(Player.position, PlayerMovement.CurrentDirection.position, Color.green);
if (Physics.Raycast(Player.position, PlayerMovement.CurrentDirection.position, out WhatsInFrontOfMe))
{
WIFOMTarget = WhatsInFrontOfMe.transform;
if(WIFOMTarget.gameObject.CompareTag("Container"))
{
CI = this.WIFOMTarget.GetComponent<ContainerItem>();
if (CI.Inspected == false)
{ GUI.Box(new Rect(300, 650, 400, 100), "Uninspected Container"); }
else
{
if (CI.Empty == true)
{ GUI.Box(new Rect(300, 650, 400, 100), "Empty"); }
else { }
}
}
if (WIFOMTarget.gameObject.CompareTag("DoorWay"))
{
DW = this.WIFOMTarget.GetComponent<DoorWay>();
Debug.Log(DW.name);
DW.Teleporting();
}
Debug.Log(WIFOMTarget.name);
}
and my movement script for references
private float MoveSpeedSlow = 0.75f;
private float MoveSpeedWalk = 1.5f;
private float MoveSpeedRun = 3f;
public bool Slow = false;
public int DirectionGUI;
public static Transform CurrentDirection;
public Transform Up;
public Transform Down;
public Transform Left;
public Transform Right;
public Transform UpLeft;
public Transform UpRight;
public Transform DownLeft;
public Transform DownRight;
private Vector2 _Move = Vector2.zero;
private CharacterController Player;
// Use this for initialization
void Start ()
{
Player = GetComponent<CharacterController>();
Up = this.transform.FindChild("AttackDirections").FindChild("U");
Down = this.transform.FindChild("AttackDirections").FindChild("D");
Left = this.transform.FindChild("AttackDirections").FindChild("L");
Right = this.transform.FindChild("AttackDirections").FindChild("R");
UpLeft = this.transform.FindChild("AttackDirections").FindChild("UL");
UpRight = this.transform.FindChild("AttackDirections").FindChild("UR");
DownLeft = this.transform.FindChild("AttackDirections").FindChild("DL");
DownRight = this.transform.FindChild("AttackDirections").FindChild("DR");
CurrentDirection = Down;
DirectionGUI = 2;
Slow = false;
}
// Update is called once per frame
void Update ()
{
if(Input.GetKey(KeyCode.UpArrow))
{
CurrentDirection = Up;
DirectionGUI = 1;
}
if (Input.GetKey(KeyCode.DownArrow))
{
CurrentDirection = Down;
DirectionGUI = 2;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
CurrentDirection = Left;
DirectionGUI = 3;
}
if (Input.GetKey(KeyCode.RightArrow))
{
CurrentDirection = Right;
DirectionGUI = 4;
}
if (Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.LeftArrow))
{
CurrentDirection = UpLeft;
DirectionGUI = 5;
}
if (Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.RightArrow))
{
CurrentDirection = UpRight;
DirectionGUI = 6;
}
if (Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.LeftArrow))
{
CurrentDirection = DownLeft;
DirectionGUI = 7;
}
if (Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.RightArrow))
{
CurrentDirection = DownRight;
DirectionGUI = 8;
}
if(Slow == true)
{
MovementSlow();
}
else if(Input.GetKey(KeyCode.RightControl))
{
MovementRun();
}
else
{
MovementWalk();
}
}
public void OnGUI()
{
switch (DirectionGUI)
{
case 1:
GUI.backgroundColor = Color.red;
GUI.Box(new Rect(515, 305, 10, 10), "");
break;
case 2:
GUI.backgroundColor = Color.red;
GUI.Box(new Rect(515, 475, 10, 10), "");
break;
case 3:
GUI.backgroundColor = Color.red;
GUI.Box(new Rect(425, 390, 10, 10), "");
break;
case 4:
GUI.backgroundColor = Color.red;
GUI.Box(new Rect(595, 390, 10, 10), "");
break;
case 5:
GUI.backgroundColor = Color.red;
GUI.Box(new Rect(465, 345, 10, 10), "");
break;
case 6:
GUI.backgroundColor = Color.red;
GUI.Box(new Rect(565, 345, 10, 10), "");
break;
case 7:
GUI.backgroundColor = Color.red;
GUI.Box(new Rect(465, 435, 10, 10), "");
break;
case 8:
GUI.backgroundColor = Color.red;
GUI.Box(new Rect(565, 435, 10, 10), "");
break;
}
}
private void MovementSlow()
{
_Move = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
_Move *= MoveSpeedSlow;
Player.Move(_Move * Time.deltaTime);
}
private void MovementWalk()
{
_Move = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
_Move *= MoveSpeedWalk;
Player.Move(_Move * Time.deltaTime);
}
private void MovementRun()
{
_Move = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
_Move *= MoveSpeedRun;
Player.Move(_Move * Time.deltaTime);
}
this pic shows the debug.drawline and the treasurechest object selected in the inspector to display its collider this seems to happen with every object I attempt to make
and another one with the debug.drawline and object selected in the inspector to display its collider
Screen position out of view frustum (screen pos 0.000000, 0.000000, 1000.000000) (Camera rect 0 0 0 0) UnityEditor.DockArea:OnGUI()
is now showing up when I reload my project
Answer by xephin2004 · Oct 04, 2015 at 01:32 PM
Changed raycast to a linecast and issue fixed
Your answer
Follow this Question
Related Questions
Raycast exit point of collider 0 Answers
Raycast goes through collider 2 Answers
Raycasting Through Terrain 1 Answer
Finding a RaycastHit tag (Null Reference Exception) 2 Answers
Raycast on a 2D Collider 7 Answers