• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by xephin2004 · Oct 02, 2015 at 05:14 PM · 2draycastcollidercollision detectionraycasthit

Physics raycast hit offset from where it should be

alt text 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 posalt textI 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);
 }

screenshot-199.png (258.8 kB)
screenshot-200.png (136.0 kB)
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xephin2004 · Oct 03, 2015 at 01:42 PM 0
Share

alt text 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

screenshot-209.png (378.6 kB)
avatar image xephin2004 · Oct 03, 2015 at 01:43 PM 0
Share

alt text and another one with the debug.drawline and object selected in the inspector to display its collider

screenshot-210.png (403.7 kB)
avatar image xephin2004 · Oct 03, 2015 at 02:42 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by xephin2004 · Oct 04, 2015 at 01:32 PM

Changed raycast to a linecast and issue fixed

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Raycast goes through collider 2 Answers

Raycast exit point of collider 0 Answers

raycastHit.point - How do I make it ignore colliders? 1 Answer

Multiple hit detection with Raycasting? 2 Answers

Detecting if the object is hit by LineRenderer 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges