• 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 /
  • Help Room /
avatar image
0
Question by TheQwantomSahdow · May 12, 2016 at 07:57 PM · raycastdetectionstealth

Raycast from evemy to player is way to far down

So I've been trying to make a sort of detection system for a game, and using raycasts to find what is in the trigger zone of an enemy, using a box as a trigger for testing. I decided to use Debug.DrawRay, and the ray just keeps going down. I think it might have something to do with how I'm calculating stuff in my character controller. Here's the code for the detection I've been working on:

  public void Alert(Collider Other) {
         Debug.Log("Start");
         Ray ray = new Ray(transform.position,Other.transform.position-transform.position);
         RaycastHit hit;
         if(Other.gameObject.tag.Equals("Player"))
             if (Physics.Raycast(ray, out hit, 4))
             {
                 Debug.Log("If1");
                 Debug.Log(hit.collider.gameObject.tag);
                 Debug.DrawRay(transform.position,Other.transform.position-transform.position,Color.cyan);
                 Debug.Log(Other.transform.position);
                 if (hit.collider.gameObject.tag.Equals("Player"))
                 {
                     Debug.Log("If2");
                     mr.material.SetColor("_Color", Color.yellow);
                 }
         }
     }

And here's the code for the character controller:

 public class PlayerControls : MonoBehaviour {
     public float mSpeed = 3.0f;
     public float mSen = 2.0f;
     public float velocity = 0.0f;
     public float jSpeed = 15.0f;
     private float camRot = 0.0f;
     public float camLimit = 60.0f;
     public bool doubleJump = true;
     public bool ground = true;
     CharacterController cc;
     // Use this for initialization
     void Start ()
     {
         Cursor.visible = false;
         cc = GetComponent<CharacterController>();
     }
     
     // Update is called once per frame
     void Update ()
     {
         //rotation
         float rotHor = Input.GetAxis("Mouse X")*mSen;
         camRot -= Input.GetAxis("Mouse Y")*mSen;
         camRot = Mathf.Clamp(camRot, -camLimit, 90.0f);
 
         transform.Rotate(0, rotHor, 0);
         Camera.main.transform.localRotation = Quaternion.Euler(camRot, 0, 0);
         //Movement
         float fSpeed = Input.GetAxis("Vertical")*mSpeed;
         float sSpeed = Input.GetAxis("Horizontal")*mSpeed;
 
         if ((cc.collisionFlags == CollisionFlags.Sides && velocity <= 0) && (fSpeed!=0|| sSpeed!=0))
             velocity += Physics.gravity.y * Time.deltaTime / 15;
         else if (cc.collisionFlags != CollisionFlags.Sides || velocity >= 0)
             velocity += Physics.gravity.y* Time.deltaTime;
         if (cc.isGrounded)
             velocity = -0.5f;
         if (Input.GetButtonDown("Jump"))
         {
             if(cc.isGrounded)
                 velocity = jSpeed;
             else if (doubleJump && cc.collisionFlags == CollisionFlags.Sides)
                 {
                     velocity = jSpeed;
                     doubleJump = false;
                 }
         }
         if (!doubleJump && cc.isGrounded)
             doubleJump = true;
         Vector3 s = new Vector3(sSpeed, velocity, fSpeed);
         s = transform.rotation * s;
         ground = cc.isGrounded;
         cc.Move(s*Time.deltaTime);
     }
 }


EDIT: Picture for clarity:alt text

ray-problem.png (223.6 kB)
Comment
Add comment · Show 4
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 FortisVenaliter · May 12, 2016 at 08:46 PM 0
Share

You do know the general definition of a ray is a line from a point to infinity in a certain direction, right? It's supposed to go on forever.

If you want a line between two points, that's not called a ray, it's called a Line Segment.

avatar image TheQwantomSahdow FortisVenaliter · May 12, 2016 at 10:21 PM 0
Share

I mean that the ray goes below where it should be pointing, not that it goes on infinitely. It results in a problem where it only hits the floor.

avatar image FortisVenaliter TheQwantomSahdow · May 12, 2016 at 10:27 PM 0
Share

$$anonymous$$aybe it would help if you included a screenshot or diagram to show the problem?

Show more comments

1 Reply

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

Answer by TheQwantomSahdow · May 13, 2016 at 09:13 PM

I had gravity on in the rigidbody of the player object.

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 hit 2 Answers

Raycast detect in object (problem) 0 Answers

Raycast to an area, not a point 3 Answers

How to make a "raycast" vertically in front of the player ? 0 Answers

Correct and efficient player detection? 1 Answer

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