• 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. W$$anonymous$$le the script used to work just fine. All of a sudden it stopped $$anonymous$$tting 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 $$anonymous$$t is Archer or not. But even if i remove that condition. The Position of the object $$anonymous$$t is always 0,0,0. There's not$$anonymous$$ng in front of the camera. The y value of my colliders are too $$anonymous$$gh.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 $$anonymous$$t;
     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 t$$anonymous$$s 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 $$anonymous$$t, 100)
             && Input.GetMouseButtonDown (0)
             //&& $$anonymous$$t.collider.gameObject.name == "Archer"
         ) { 
             unitSelected = true;
             Debug.Log ("{0}", $$anonymous$$t.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 = $$anonymous$$t.transform.position.x;
             targetZ = $$anonymous$$t.transform.position.z;
         }
     /* Deneme aşamasında ama yemiyor
         if (Input.GetMouseButtonDown (0)) {
             if ($$anonymous$$t.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
avatar image caylkadam · Apr 05, 2016 at 06:11 PM 0
Share
avatar image NonBreaker caylkadam · Apr 05, 2016 at 06:26 PM 0
Share

0 Replies

  • Sort: 

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

RigidBody Collisions Detect 0 Answers

Sphere bouncing back on edges of aligned objects 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