Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

  • 0
Sign in to follow this  
CakeMonsTv

Problem with finding distance between objects on specific event

Question

Hello, So i want to find out distance between position where first ray from input.mousePosition hit terrain (that happens when mouse button 1 is held down) and position from second ray  from input.mouseposition hit terrain (that happens when mouse button 1 is let go). For some reason that Getmousebutton doesnt work or something. Really weird. Can someone please help me out. Ive been trying to solve this problem for like 3 hours now cant cant get my head around it...   Go to line of code that says  THIS PART OF CODE IS NOT WORKING.   Thanks :)

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FormationCoNtrol : MonoBehaviour {
    public GameObject formation;
    public string formationTag;
    public GameObject place1;
    public GameObject place2;
    public GameObject place3;
    public GameObject place4;
    public GameObject place5;
    public GameObject place6;
    public GameObject place7;
    public GameObject place8;
    public GameObject place9;
    public GameObject place10;
    public GameObject place11;
    public GameObject place12;
    public Availability isFreeScript;
    public CameraControl cam;
    public bool isSelected = false;
    public string team;
    public int unitCount = 12;
    public int placeDist = 0;
    public GameObject places;

    // Use this for initialization
    void Start () {

        formation = GameObject.FindGameObjectWithTag (formationTag); //GO TO MOUSE CLICK POSITION
        cam = GameObject.Find ("Main Camera").GetComponent<CameraControl>();
    }
    
    // Update is called once per frame
    void Update () {
        Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        RaycastHit hit;

        if (Input.GetMouseButtonDown (0) && isSelected == true) {
            if (Physics.Raycast (ray, out hit, 100)) {
                if (hit.transform.tag == team) { //if ray hit object, and it has the same name as string team..

                } else {
                    isSelected = false;
                }
            }
        }
            
        if (Input.GetMouseButtonDown (1) && isSelected == true) {
            if (Physics.Raycast (ray, out hit, 100)) {
                formation.transform.position = hit.point;
                transform.eulerAngles = new Vector3 (0, cam.rot, 0); 


                place1.tag = "place";
                place2.tag = "place";
                place3.tag = "place";
                place4.tag = "place";
                place5.tag = "place";
                place6.tag = "place";
                place7.tag = "place";
                place8.tag = "place";
                place9.tag = "place";
                place10.tag = "place";
                place11.tag = "place";
                place12.tag = "place";


                place1.GetComponent<Availability> ().isFree = true;
                place2.GetComponent<Availability> ().isFree = true;
                place3.GetComponent<Availability> ().isFree = true;
                place4.GetComponent<Availability> ().isFree = true;
                place5.GetComponent<Availability> ().isFree = true;
                place6.GetComponent<Availability> ().isFree = true;
                place7.GetComponent<Availability> ().isFree = true;
                place8.GetComponent<Availability> ().isFree = true;
                place9.GetComponent<Availability> ().isFree = true;
                place10.GetComponent<Availability> ().isFree = true;
                place11.GetComponent<Availability> ().isFree = true;
                place12.GetComponent<Availability> ().isFree = true;

            }
        }

        if (Input.GetMouseButton (1) && isSelected == true){                                                       //THIS PART OF CODE IS NOT WORKING
            Ray ray3 = Camera.main.ScreenPointToRay (Input.mousePosition); 
            RaycastHit hit3;
            if (Physics.Raycast (ray3, out hit3, 100)) {
                places.transform.position = hit3.point;
            }
        if (Input.GetMouseButtonUp (1)) {
                Ray ray2 = Camera.main.ScreenPointToRay (Input.mousePosition);                                        //THIS PART OF CODE IS NOT WORKING
                RaycastHit hit2;

                if (Physics.Raycast (ray2, out hit2, 100)) {
                    
                    float distance = Vector3.Distance(hit2.point,  hit3.point);    
                    Debug.Log ("distance is" + distance);                                                          //THIS PART OF CODE IS NOT WORKING

                }
            }
        }
    }

    public void Selected () {
        isSelected = true;
    }

}

Share this post


Link to post
Share on other sites

8 answers to this question

Recommended Posts

  • 0

Figured it out. So I dont even know what the real reason was but i just changed script for ray shooting and the other reason that im sure of was that you cant put getmousebuttonup insde getmousebutton.

 

void Update () {
        if (Input.GetMouseButtonDown (1)) {          
            
            RaycastHit hit;
            if (Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hit, 100)) {
                Pressed = true;
                places.transform.position = hit.transform.position;
            }
        }

            if (Input.GetMouseButtonUp (1) && (Pressed == true)) {
            
            Pressed = false;

            RaycastHit hit;
            if (Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hit, 100)) {
                
                float distance = Vector3.Distance(hit.point,  places.transform.position);    
                Debug.Log ("distance is" + distance); 

                Pressed = false;
            }

        }
        
    }

Share this post


Link to post
Share on other sites

  • 0

When do you call Selected() (the function that makes selected true). The problem is not the mouse button, it's that selected is not set to true

Share this post


Link to post
Share on other sites
  • 0

 

Please login or register to see this quote.

I call it from another script attached to my unit. If i click on my unit, it calls selected function. And i checked, sisSelected is true and it still doesnt work. 

Share this post


Link to post
Share on other sites
  • 0

I'm not seeing anywhere else that could be be the issue unless it is is being called but the rays messed up. Where you have the comment (THIS PART OF CODE NOT WORKING) put Debug.Log("called"); and see if it prints to the console. If it isn't being called, then put Debug.Log("isSelected is " + isSelected.ToString()); right before the if statement

  • Thanks 1

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

Alright. So, I did what you said and it wrote in a log right after first if statement. Then i put it after second if statement, and it print out anything anymore.

Share this post


Link to post
Share on other sites
  • 0

Looking at your code again and it looks like your asking for hit3.point which doesn't exist because it belongs to the previous if statement. Put hit3 variable outside of the if statement so it's not confined to that if block

  • Like 1

Share this post


Link to post
Share on other sites
  • 0

I will test that too, but I think I figured it out. The second if statement cannot be called since mouse button 1 isnt pushed at the moment, therefore it cant be in first if statement which requires button to be held all the time. I didn't test it yet tho, ill let you know.

Please login or register to see this quote.

 

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

Nope. None of those solutions work... I tried replacing hit3.point with places.transform.position and i also tried putting second if statement out of first one and checking if first one was true before, then the program can call the second one. but it doesnt work. Any other ideas on how i could get the same effect. I mean i think there is a bug in unity's part

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...