- Home /
Odd Behavior with my Drag and Drop Code
Hey so I just started working on a drag and drop construction system, where the user can choose a structure and click and drag the position and length of the wall he is creating(think age of empires wall system) so far the drag and drop works well enough for isolated segments of walls, but anytime I try building off an already instantiated wall, the wall segment gets destroyed 80% of the time, and the rest works about 30% of the time, which confuses me on why my code would work so unreliably. Here's the class I use to create structures
class CreateStructure extends MonoBehaviour
{
//test Prefab
public var testSegment:GameObject;
public var testConnector:GameObject;
//Start and finish Locations of mouse drag
var startPos:Vector3;
var endPos:Vector3;
//Build Camera
var topCam:Camera;
function Start()
{
topCam = GameObject.FindGameObjectWithTag("Build Camera").camera;
testSegment = AssetDatabase.LoadAssetAtPath("Assets/Prefabs/WallSection1.prefab", GameObject);
testConnector = AssetDatabase.LoadAssetAtPath("Assets/Prefabs/WallConnector1.prefab",GameObject);
}
function Update()
{
//when mouse clicked down, store starting position
if(Input.GetMouseButtonDown(0))
{
startPos = FindLocation();
}
//when mouse button released, store ending position
if(Input.GetMouseButtonUp(0))
{
endPos = FindLocation();
CalculateAndPlace(startPos, endPos, testSegment);
}
}
function FindLocation() : Vector3
{
//raycast based on mouse location
var ray : Ray = topCam.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if(Physics.Raycast(ray, hit))
{
//if no structures returned from raycast, return point
if(hit.transform.tag == "Ground")
return hit.point;
//if structure returned, remove that structure
if(hit.transform.tag == "Small Wall" || hit.transform.tag == "Wall Connector")
{
return ReplaceWall(hit.transform.gameObject);
}
}
return hit.point;
}
function CalculateAndPlace(p1 : Vector3, p2 : Vector3, seg: GameObject)
{
//Create a ray long the path of the two points starting at startPos
var temp:GameObject = new GameObject();
temp.transform.position = p1;
temp.transform.LookAt(p2);
var directionRay : Ray = new Ray(p1, temp.transform.forward);
//calculate distance between p1 and p2
var distance :float = Vector3.Distance(p1,p2);
//calculate number structures to be placed
var numberOfStructures : int;
var segmentLength = seg.renderer.bounds.size.z;
numberOfStructures = distance / segmentLength;
//place structures along the directionRay
for(var i : int = 0; i <= numberOfStructures; i++)
{
PlaceStructure(directionRay.GetPoint(segmentLength * i), temp, seg);
}
}
function PlaceStructure(position : Vector3, tempObj: GameObject, seg : GameObject)
{
Instantiate(seg, position, tempObj.transform.rotation);
}
//replace wall With connector prefab
function ReplaceWall(obj : GameObject) : Vector3
{
//store obj components
var tempTras :Vector3= obj.transform.position;
var tempRot :Quaternion = obj.transform.rotation;
//raise translate up 10 so raycast wont be below the ground
tempTras.y += 10;
//store ray to return
var point : Vector3;
//destroy object so raycast wont hit it
Destroy(obj);
//raycast to find position
var hit : RaycastHit;
if(Physics.Raycast(obj.transform.position, Vector3.down, hit))
{
point = hit.point;
}
//instantiate the wall connector prefab
Instantiate(testConnector, obj.transform.position, obj.transform.rotation);
//return the raycast position
return point;
}
}
Anyone have any ideas whats wrong with my code or a better system for drag and drop all together?
Ok so I figured out the problems with the code and got it working 100%,so guess i got this covered
Your answer
Follow this Question
Related Questions
How to activate a button? 1 Answer
Changing variable in a script of a game object hit with raycast 1 Answer
[SOLVED] Only instantiating once 1 Answer
Raycast help? 1 Answer
A way to instantiate an item with 90 Degree increments? 2 Answers