Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ImPvEspec · May 03, 2013 at 05:54 AM · raycastselectionmouse-drag

area selection (mouse drag) on overhead game

I have a single unit selection script that a friend and myself made and neither of us know how to make it into area drag, had a bit of a look and is proving a tad more challenging than anticipated. The camera is orthographic with an overhead view, using 3D models on a flat plane.

 using UnityEngine;
 using System.Collections;
 
 public class Select : MonoBehaviour {
     private GameObject lastHitObj;
     private GameObject selectedObj;
     public LayerMask placementLayerMask;
     public LayerMask ground;
     private RaycastHit hit;
     private RaycastHit pos;
     public Material mouseOver;
     public Material mouseExit;
     public float speed = 5.0f;
     private Vector3 moveTo;
     private bool moveing = true;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if(selectedObj){
             MoveObject();
         }
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition); //create ray on mouse pos
         if(Physics.Raycast (ray,out hit,1000,placementLayerMask)){
             if(lastHitObj){
                 if(Input.GetMouseButtonDown(0)){
                     lastHitObj.renderer.material.color = Color.white;
                     selectedObj = lastHitObj.transform.parent.gameObject;
                 }
             }
             lastHitObj = hit.collider.gameObject;
             //mouseExit = lastHitObj.renderer.material;
             lastHitObj.renderer.material.color = Color.green;//set materal to hover mat
         }
         else{ //not on building block
             if(lastHitObj){// if previously hit something
                 lastHitObj.renderer.material.color = Color.white;
                 lastHitObj = null;
             }    
         }
     }
     
     void MoveObject(){
     
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition); //create ray on mouse pos
             if(Physics.Raycast (ray,out pos,1000,ground)){
                 if(Input.GetMouseButtonDown(1)){
                 Debug.Log ("Running");
                 moveTo = pos.point;
                 if(moveing){
                     StartCoroutine (Moveing());
                     moveing = false;
                 }
                 
             }
         }
     }
     IEnumerator Moveing(){
         while(selectedObj.transform.position != moveTo){
             
             selectedObj.transform.position = Vector3.MoveTowards(selectedObj.transform.position, moveTo, speed * Time.deltaTime);
             selectedObj.transform.LookAt( new Vector3 (moveTo.x,moveTo.y,moveTo.z));
             yield return new WaitForSeconds(0.002f);
         }
         moveing = true;
     }
 }
 
Comment
Add comment
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

1 Reply

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

Answer by Fattie · May 03, 2013 at 06:17 AM

So go here and check out whydoidoit's answer,

http://answers.unity3d.com/questions/433757/using-rectcontains-to-check-against-world-coordina.html

hope it helps!

BTW it's very hard to convert between all the paradigms involved

http://answers.unity3d.com/questions/292333/how-to-calculate-swipe-speed-on-ios.html

Comment
Add comment · Show 6 · 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
avatar image Fattie · May 03, 2013 at 06:43 AM 0
Share

is it EXACTLY top down? or is the camera on a slight grazing angle ?

are they 3D models or just sprites ?

avatar image ImPvEspec · May 03, 2013 at 07:06 AM 0
Share

Hope the picture helps The camera's Orthographic so I assume that the way I've presented it makes it top-down, but the assets are 3D models sat on a flat plane.

alt text

gamescreenshots.jpg (340.6 kB)
avatar image Fattie · May 03, 2013 at 07:11 AM 0
Share

well cool -- amazing how much time is wasted on this site when "nobody has a clue what is being discussed"

WHy not edit that in to your actual question?

("top" down could possible mean literally from the top. ie the camera is pointing exactly vertically downwards. the ter$$anonymous$$ology is obscure. here as video game engineers, everyone should simply state "the camera is orthographic, and is at a 45 degree downwards angle from horizontal, and is located 35 meters above the ground surface")

$$anonymous$$gest just read what whydoidoit said in his answer on linked question

avatar image Fattie · May 03, 2013 at 07:11 AM 0
Share

sorry i've just realised it's exactly overhead, seeing your notation in the image. anyways great, again look to whydoidoit's answer and my long answer on converting coirdinates

avatar image ImPvEspec · May 03, 2013 at 08:16 AM 0
Share

Is there a beginners answer? :( Sorry I hate to feel as if I'm wasting your time on stupid shit, but I'm still quite new and I just don't understand, probably should have chosen an easier game for my assignment.

Show more comments

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

13 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

Related Questions

How do I make a Raycast attached to my mouse rotate an object with a script attached on the Y axis? 0 Answers

Unity RayCast Selection 1 Answer

GUI Box not showing up after object clicked 1 Answer

Add force to the selected object 1 Answer

I am trying to pick up an Object and drag it with my mouse. 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