- Home /
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;
}
}
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
is it EXACTLY top down? or is the camera on a slight grazing angle ?
are they 3D models or just sprites ?
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.
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
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
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.
Your answer
Follow this Question
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