problems with drag start program for bowling game
I added if (! ball.inPlay) to Dragstart and DragEnd to keep the ball from being moved after launch. now ball wont launch at all. it woild launch beforer with if the if (! ball.inplay) was only under MoveStart, but you could still move the ball after launch with the mouse how can I keep the mouse from controlling the ball after launch
using UnityEngine; using System.Collections; [RequireComponent (typeof(Ball))] public class DragLaunch : MonoBehaviour { private Ball ball; private Vector3 dragStart, dragEnd; private float startTime, endTime;
// Use this for initialization
void Start () {
ball = GetComponent<Ball>();
}
public void MoveStart (float amount)
{
if (! ball.inPlay)
{
ball.transform.Translate(new Vector3(amount, 0, 0));
}
}
public void DragStart () {
if (!ball.inPlay)
{
dragStart = Input.mousePosition;
startTime = Time.time;
}
}
public void DragEnd ()
{
if (!ball.inPlay)
{
dragEnd = Input.mousePosition;
endTime = Time.time;
float dragDuration = endTime - startTime;
float launchSpeedX = (dragEnd.x - dragStart.x) / dragDuration;
float launchSpeedZ = (dragEnd.y - dragStart.y) / dragDuration;
Vector3 launchVelocity = new Vector3(launchSpeedX, 0, launchSpeedZ);
ball.Launch(launchVelocity);
}
}
}
HERE IS THE BALL SCRIPT
using UnityEngine; using System.Collections;
public class Ball : $$anonymous$$onoBehaviour { public Vector3 launchVelocity; public bool inPlay = false; public Vector3 ballStartPos;
private Rigidbody rigidBody;
private AudioSource audioSource;
void Start() {
rigidBody = GetComponent<Rigidbody>();
rigidBody.useGravity = false;
ballStartPos = transform.position;
}
public void Launch (Vector3 velocity){
inPlay = true;
rigidBody.useGravity = true;
rigidBody.velocity = velocity;
audioSource = GetComponent<AudioSource>();
audioSource.Play();
//inPlay = true;
}
public void Reset ()
{
inPlay = false;
transform.position = ballStartPos;
transform.rotation = Quaternion.Euler(0, 0, 0);
rigidBody.velocity = Vector3.zero;
rigidBody.angularVelocity = Vector3.zero;
rigidBody.useGravity = false;
}
}
Your answer

Follow this Question
Related Questions
New to IF statements. Need help with playing animation and using IF Statements. 1 Answer
Error Instantiating Prefab in while look at end of row, Happens after a random amount of rows 0 Answers
Other way to write a code 0 Answers
Problem with If statement and Collision!? 0 Answers
add score when collide with gameobject 0 Answers