Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

raycast going through walls 0 Answers

how make raycast detect in object for crosshair 0 Answers

Is there a better way to check if a player is looking at a certain object? 0 Answers

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

Raycast hit 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges