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 /
avatar image
0
Question by spinnerbox · Sep 25, 2015 at 10:26 AM · collidercollision detectionraycastinglinerendererraycasthit

Using ray cast does not produce reasonable results

I want to detect when i am hitting my planets with the LineRenderer between the two ships. So I cast a ray that travels on the same line as the line renderer shown in the images. Line renderer is colored yellow.

hit planet earth

I this image if you see bottom left the console prints hit planet Earth.

hit planet earth 2

In this second image you see from top that the line renderer is between the planets and also the ray casted. If you check bottom left you will see that the console prints that the ray hit Earth2 but visualy it does not look like so.

Here is the code I am using for my GameController object. It is empty game object btw and it should controll the ray casting and line renderer.

 using UnityEngine;
 using System.Collections;
 
 public class GameController : MonoBehaviour 
 {
 
     public GameObject spaceShipSelected;
     public GameObject spaceShip1;
     public GameObject spaceShip2;
     public float spaceShipSpeed = 6f;
 
     Ray energyRay;
     RaycastHit rayHit;
     bool hittingPlanet = false;
     GameObject planetHit;
     float planetRadius;
     LineRenderer energyBeam;
     Vector3 movement;
     Vector3 spaceShip1Pos;
     Vector3 spaceShip2Pos;
 
     const string HORIZONTAL_AXIS = "Horizontal";
     const string VERTICAL_AXIS = "Vertical";
     const string MOUSE_SCROLL_WHEEL = "Mouse ScrollWheel";
 
     void Awake ()
     {
         spaceShip1Pos = spaceShip1.transform.position;
         spaceShip2Pos = spaceShip2.transform.position;
         energyBeam = GetComponent<LineRenderer> ();
     }
     
     // Update is called once per frame
     void Update () 
     {
         if (spaceShipSelected != null) 
         { 
             Debug.Log(spaceShipSelected.name);
             assignShipMovement();
         }
 
         UpdateEnergyBeam();
     }
 
     void assignShipMovement ()
     {
         float h = Input.GetAxisRaw(HORIZONTAL_AXIS);
         float m = Input.GetAxisRaw(MOUSE_SCROLL_WHEEL);
         float v = Input.GetAxisRaw(VERTICAL_AXIS);
 
         movement.Set(h, m, v);
         movement = movement.normalized * spaceShipSpeed * Time.deltaTime;
 
         spaceShipSelected.transform.position += movement;
     }
 
     void UpdateEnergyBeam ()
     {
         spaceShip1Pos = spaceShip1.transform.position;
         spaceShip2Pos = spaceShip2.transform.position;
 
         energyBeam.SetPosition(0, spaceShip1Pos);
         energyBeam.SetPosition(1, spaceShip2Pos);
 
         energyRay = new Ray(spaceShip1Pos, spaceShip2Pos);
         if(Physics.Raycast(energyRay, out rayHit, Vector3.Distance(spaceShip1Pos, spaceShip2Pos)))
         {
 
             planetRadius = Vector3.Distance(rayHit.collider.bounds.center, rayHit.collider.bounds.extents);
 
             Debug.Log ("I hit a planet: " + rayHit.collider.name);
         }
     }
 }

Note: To be more clear, I made the spaceShipSelected set to the game object (grey boxes) I select collored in yellow when I press on one of the boxes.

Any reason why this happens?

spaceships2.png (67.1 kB)
spaceships3.png (67.2 kB)
Comment
Add comment · Show 1
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 sandeepsmartest · Sep 25, 2015 at 12:06 PM 0
Share

Also apart from @suddoha's suggestion try checking the collider's radius.it might be bigger than the actual sphere(just in case)

1 Reply

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

Answer by Suddoha · Sep 25, 2015 at 10:42 AM

Seems to be the typical mistake that's done quite often.

You use a ray which takes start position and direction as arguments, both being vectors. But you've passed the position of the second spaceship as a direction.

Try to use spaceShip2Pos - spaceShip1Pos as the second parameter.

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

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast exit point of collider 0 Answers

Restrict Rigidbody movement in the Z axis 1 Answer

getting raycast working on prefabs made from resources created at runtime 1 Answer

Physics raycast hit offset from where it should be 1 Answer

How can I let the ray to the edge of the capsule? 1 Answer


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