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 /
This question was closed Apr 05, 2016 at 06:12 PM by caylkadam for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by caylkadam · Mar 30, 2016 at 08:43 PM · collisionerrorraycastbugcollision detection

Raycasting Stopped Working all of a sudden

Hello, i am using a script to select units with raycasting. While the script used to work just fine. All of a sudden it stopped hitting both the terrain and the objects. I have many objects with colliders on them. I haven't changed the lenght of the ray, nor the camera position. I am checking in the code whether the name of the object that's hit is Archer or not. But even if i remove that condition. The Position of the object hit is always 0,0,0. There's nothing in front of the camera. The y value of my colliders are too high.20 of x value is equal to my 17.500 of y value on colliders only.. Don't know what causes them to shrink so much. Edit: I figured it only works if the collider is long enough towards the camera, but then i have to make it gigantic. I tried using mathf.Infinity Please help me. Here is the code that uses raycasting.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 public class UnitSelectArcher : MonoBehaviour {
     Ray ourRay;
     RaycastHit hit;
     public Camera MainCamera;
     public bool unitSelected=false;
     public Button MoveButton;
     public Button AttackButton;
     public Canvas ActionMenu;
     public string unitName="Archer";
     float targetX;
     float targetZ;
     bool Moving=false;
     public float speed=2f;
     public GameObject kup;
     public Transform spawnPoint;
 
     // Use this for initialization
     void Start () {
         MainCamera = MainCamera.GetComponent<Camera> ();
         MoveButton = MoveButton.GetComponent<Button> ();
         AttackButton = AttackButton.GetComponent<Button> ();
         ActionMenu.enabled = false;
         targetX=transform.position.x;
         targetZ = transform.position.z;
     }
 
     // Update is called once per frame
     void Update () {
         ourRay = MainCamera.ScreenPointToRay (Input.mousePosition);
         if (Physics.Raycast (ourRay, out hit, 100)
             && Input.GetMouseButtonDown (0)
             //&& hit.collider.gameObject.name == "Archer"
         ) { 
             unitSelected = true;
             Debug.Log ("{0}", hit.transform);
 
         }
         
         if (unitSelected == true) {
 
             ActionMenu.enabled = true;
         }
             MoveButton.onClick.AddListener (MoveButton_Clicked);
             // The Object clicked on moves to the Target Location
         if (Input.GetMouseButtonDown (0) && Moving == true) {
             targetX = hit.transform.position.x;
             targetZ = hit.transform.position.z;
         }
     /* Deneme aşamasında ama yemiyor
         if (Input.GetMouseButtonDown (0)) {
             if (hit.collider.transform.name == name)
                 unitSelected = true;
             else {
                 unitSelected = false;
                 Moving = false;
             }
         }
     */
         if (Moving == true ) {
             
             //Debug.Log (string.Format ("targetX:{0}, targetZ:{1}", targetX, targetZ));
             if (targetX > transform.position.x)
                  {
                     Vector3 temp = new Vector3 (0.1f, 0, 0);
                     transform.position += temp;
 
                 }
             else if (targetX < transform.position.x)
                 {
                     Vector3 temp = new Vector3 (0.1f, 0, 0);
                     transform.position -= temp;
                 }
             if (targetZ > transform.position.z) {
                 Vector3 temp = new Vector3 (0, 0, 0.1f);
                 transform.position += temp;
 
             }
             else if (targetZ < transform.position.z)
             {
                     Vector3 temp = new Vector3 (0, 0, 0.1f);
                     transform.position -= temp;
         }
             unitSelected = false;
         }
 
         }
     public void MoveButton_Clicked()
         {
         Moving = true;
         }
 }


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 NonBreaker · Apr 04, 2016 at 05:52 PM 0
Share

What about checking for mouse input and raycasthit separately?

So change it to:

 if (Input.Get$$anonymous$$ouseButtonDown(0)){
     ourRay = $$anonymous$$ainCamera.ScreenPointToRay (Input.mousePosition);
     
     if (Physics.Raycast (ourRay, out hit, $$anonymous$$athf.Infinity)){
         
         Debug.Log ("{0}", hit.transform.gameObject.name);
         if (hit.transform.gameObject.name == "Archer"){
              unitSelected = true;
         }
  
     }
 }

I'm not sure if this will help at all, but I think breaking it into separate events will help find the problem.

NonBreaker

avatar image caylkadam · Apr 05, 2016 at 06:11 PM 0
Share

I solved the problem by copying everything in the scene to a new scene. I think there had been some kind of bug with that scene.

avatar image NonBreaker caylkadam · Apr 05, 2016 at 06:26 PM 0
Share

Awesome! Glad you got it working.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Raycast on moving object? 2 Answers

How i can fix a player controller bug? 0 Answers

2D collision trouble, character falling through map 1 Answer

Objects wont stop bouncing if they fall. 0 Answers

Problem with DontGoThroughThings script 0 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