Oh yeah, another idea from the scripting forums… “How do you drag something…” I am sure it’s been covered a million times but I wanted to see if my “concept” that I posted worked. Well it did, (why else would I be posting this here)
Working Demo
http://lod3dx.net/Unity/Bowling.html
What is it:
Its a basic Bowling demo, no sound but some textures.
Is it finished:
No, and probably never will be. I was just putzing around.
If I wanted to get the code to build my own, can I:
Absolutely. This was like an hours worth of tinkering and 2 hours of playing… ok, I was at work so that’s 3 hours of playing… but my boss doesn’t know… ![]()
What does it take to make it work:
2 camera placement nodes (tells the position and rotation of the main view (0) and the view that you see when the ball is hitting the pins (1))
1 camera, 1 light, a lane, some dimensions a ball, 10 pins tagged “Pin”
No, I am not going to give you everything, look at the code and figure it out. I will tell you my lane was based on a 1 unit ball and some pins scaled to match. I had to do speed and velocity checks to prevent the user from being able to hurl the ball a million miles per hour down the lane.
private var isDragging : boolean = false;
private var dragPlane : Plane;
private var moveTo : Vector3;
var dragDamper : float = 5.0;
var addToY : float = 5.0;
private var minZ : float = -48.5;
private var maxZ : float = 2;
var cameraPos : Transform[];
private var isBowling = false;
private var bowlStart = 0.0;
private var pins : GameObject[];
private var pinInits : Vector3[];
private var bowlSet = 0;
private var init : Vector3;
private var score = "";
private var pinsUp = 0;
function Start(){
pins = GameObject.FindGameObjectsWithTag("Pin");
pinInits = new Vector3[pins.Length];
for(var i = 0; i<pins.Length; i++){
pinInits[i] = pins[i].transform.position;
}
Camera.main.transform.position = cameraPos[0].position;
Camera.main.transform.rotation = cameraPos[0].rotation;
init = transform.position;
}
function Update(){
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
var dist : float;
var velocity : Vector3 = rigidbody.velocity;
if(Input.GetMouseButtonDown(0) transform.position.z < maxZ){
if(Physics.Raycast(ray, hit)){
if(hit.transform.root.transform == transform){
isDragging = true;
rigidbody.useGravity = false;
// defined drag plane:
// either directional based on the camera
//dragPlane = new Plane(-ray.direction.normalized, hit.point);
// or spacial based on the current position + addToY
var pos = transform.position;
pos.y = 1.4;
dragPlane = new Plane(Vector3.up, pos);
isBowling = false;
}
}
}
if(isDragging){
var hasHit = dragPlane.Raycast(ray, dist);
if(hasHit){
moveTo = ray.GetPoint(dist);
moveTo.z = Mathf.Clamp(moveTo.z, minZ, maxZ);
}
bowlStart = Time.time;
}
if(Input.GetMouseButtonUp(0) isDragging){
isDragging = false;
rigidbody.useGravity = true;
velocity.x = Mathf.Clamp(velocity.x, -70, 70);
velocity.y = 0;
velocity.z = Mathf.Clamp(velocity.z, 0, 70);
if(velocity.magnitude < 25.0)
velocity = velocity.normalized * 25.0;
rigidbody.velocity = velocity;
isBowling = true;
}
if(transform.position.z > maxZ){
Camera.main.transform.position = cameraPos[1].position;
Camera.main.transform.rotation = cameraPos[1].rotation;
} else {
Camera.main.transform.position = cameraPos[0].position;
Camera.main.transform.rotation = cameraPos[0].rotation;
}
if(transform.position.z > 57){
transform.position.z = 57;
rigidbody.velocity.z = -rigidbody.velocity.z;
}
var timer = Time.time - bowlStart;
if(isBowling (timer > 10.0 || (transform.rigidbody.velocity.magnitude < 0.1 timer > 5.0))){
transform.position = init;
rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;
isBowling = false;
bowlSet++;
var lastPinsUp = pinsUp;
var lastNPinsUp = 10 - pinsUp;
pinsUp =0;
for(var i=0; i<pins.Length; i++){
var up = Vector3.Dot(Vector3.up, pins[i].transform.up);
dist = Vector3.Distance(pins[i].transform.position, pinInits[i]);
if(up > 0.9 dist < 0.5){
pins[i].transform.position = pinInits[i];
pins[i].transform.up = Vector3.up;
pins[i].rigidbody.velocity = Vector3.zero;
pins[i].rigidbody.angularVelocity = Vector3.zero;
pinsUp++;
} else {
pins[i].transform.position = pinInits[i] + Vector3.right * 1000.0;
pins[i].transform.up = Vector3(0,1,1);
pins[i].rigidbody.velocity = Vector3.zero;
pins[i].rigidbody.angularVelocity = Vector3.zero;
}
}
var nPins = 10 - pinsUp;
score = "" + nPins;
if(pinsUp == 0 bowlSet == 1) score = "Strike!!!";
if(bowlSet == 2) score = lastNPinsUp + " + " + (nPins - lastNPinsUp) + " = " + nPins;
if(pinsUp == 0 bowlSet == 2) score = "Spare!!!";
if(bowlSet == 1 pinsUp == 0)
bowlSet = 2;
if(bowlSet == 2){
bowlSet = 0;
for(i=0; i<pins.Length; i++){
pins[i].transform.position = pinInits[i];
pins[i].transform.up = Vector3.up;
pins[i].rigidbody.velocity = Vector3.zero;
pins[i].rigidbody.angularVelocity = Vector3.zero;
}
}
}
}
function FixedUpdate(){
if(!isDragging) return;
var velocity = (moveTo - transform.position) * 2.0;
if(velocity.magnitude > 70) velocity = velocity.normalized * 70;
if(transform.position.x < -6.25){
transform.position.x = -6.25;
velocity.x = Mathf.Clamp(velocity.x, 0, 70);
}
if(transform.position.x > 6.25){
transform.position.x = 6.25;
velocity.x = Mathf.Clamp(velocity.x, -70, 0);
}
rigidbody.velocity = Vector3.Lerp(rigidbody.velocity, velocity, dragDamper * Time.deltaTime);
}
function OnGUI(){
GUI.Label (Rect (10, 10, 100, 20), "" + score);
}
@script RequireComponent(Rigidbody)
It’s fun, its easy and I hope you guys enjoy it.