- Home /
How to not allow ball to be thrown after first Toss
I want to be able to drag and throw my ball, then after I throw it 1 Time not be able to throw it again Until the player presses R to restart the ball Position if anyone has any ideas on how to do that could you show me what to change and explain whats going on thank you in advanced trying to learn code. I have been trying to follow youtube tutorials but they don't seem to be helping or explaining the things I want to know or have questions about. so if anyone has any ideas that would be great
using UnityEngine;
using System.Collections;
using System;
namespace Shorka.BallTrajectory
{
public enum PullState
{
Idle,
UserPulling,
ObjFlying
}
//Pulling/Release
public class PullCtrl : MonoBehaviour
{
#region fields
//[SerializeField] private Transform throwTarget;
[SerializeField] private ThrownObject throwObj;
[SerializeField] private Transform dotHelper;
[SerializeField] private Transform pullingStartPoint;
[Space(5)]
[Tooltip("this linerenderer will draw the projected trajectory of the thrown object")]
[SerializeField]
private LineRenderer trajectoryLineRen;
[SerializeField]
private TrailMaker trail;
[Space(5)]
[SerializeField]
private float throwSpeed = 10F;
[Tooltip("Max Distance between 'PullingStartPoint' and pulling touch point")]
[SerializeField]
private float maxDistance = 1.5F;
[SerializeField]
private float coofDotHelper = 1.5F;
[Tooltip("Related to length of trajectory line")]
[SerializeField]
private int qtyOfsegments = 13;
[Tooltip("Step of changing trajectory dots offset in runtime")]
[SerializeField]
private float stepMatOffset = 0.01F;
[Tooltip("Z position of trajectory dots")]
[SerializeField]
private float dotPosZ = 0F;
private PullState pullState;
private Camera camMain;
//private Collider2D collThrowTarget;
private Rigidbody2D rgThrowTarget;
private Vector3 posPullingStart;
private Vector3 initPos;
private TrajectoryCtrl trajCtrl;
#endregion
public Vector3 PosDotHelper { get { return dotHelper.position; } }
public Vector3 PosThrowTarget { get { return throwObj.transform.position; } }
public int QtyOfsegments { get { return qtyOfsegments; } }
public float DotPosZ { get { return dotPosZ; } }
public Vector3 PosPullingStart { get { return posPullingStart; } }
public float StepMatOffset { get { return stepMatOffset; } }
void Awake()
{
trail.emit = false;
trajCtrl = new TrajectoryCtrl(this, trajectoryLineRen);
}
void Start()
{
camMain = Camera.main;
pullState = PullState.Idle;
posPullingStart = pullingStartPoint.position;
initPos = PosThrowTarget;
}
void Update()
{
SwitchStates();
if (Input.GetKeyDown(KeyCode.R))
{
Debug.Log("Restart object states");
Restart(initPos);
}
}
private void SwitchStates()
{
switch (pullState)
{
case PullState.Idle:
if (Input.GetMouseButtonDown(0))
{
//get the point on screen user has tapped
Vector3 location = camMain.ScreenToWorldPoint(Input.mousePosition);
//if user has tapped onto the ball
if (throwObj.Collider == Physics2D.OverlapPoint(location))
pullState = PullState.UserPulling;
}
break;
case PullState.UserPulling:
dotHelper.gameObject.SetActive(true);
if (Input.GetMouseButton(0))
{
//get touch position
Vector3 posMouse = camMain.ScreenToWorldPoint(Input.mousePosition);
posMouse.z = 5; //I CHANGED THIS FROM 0 to 5////////////////////////////////// YEET
//we will let the user pull the ball up to a maximum distance
if (Vector3.Distance(posMouse, posPullingStart) > maxDistance)
{
Vector3 maxPosition = (posMouse - posPullingStart).normalized * maxDistance + posPullingStart;
maxPosition.z = dotHelper.position.z;
dotHelper.position = maxPosition;
}
else
{
posMouse.z = dotHelper.position.z;
dotHelper.position = posMouse;
}
float distance = Vector3.Distance(posPullingStart, dotHelper.position);
trajCtrl.DisplayTrajectory(distance);
}
else//user has removed the tap
{
float distance = Vector3.Distance(posPullingStart, dotHelper.position);
trajectoryLineRen.enabled = false;
ThrowObj(distance);
}
break;
default:
break;
}
}
//private Vector2 velocityToRg = Vector2.zero;
private void ThrowObj(float distance)
{
Debug.Log("ThrowObj");
pullState = PullState.Idle;
Vector3 velocity = posPullingStart - dotHelper.position;
//velocityToRg = CalcVelocity(velocity, distance);
throwObj.ThrowObj(CalcVelocity(velocity, distance));
//rgThrowTarget.velocity = velocityToRg;
//rgThrowTarget.isKinematic = false;
trail.enabled = true;
trail.emit = true;
dotHelper.gameObject.SetActive(false);
}
/// Restart thrown object states, clear trail
/// <param name="posThrownObj"></param>
public void Restart(Vector3 posThrownObj)
{
trail.emit = false;
trail.Clear();
StartCoroutine(ClearTrail());
trajectoryLineRen.enabled = false;
dotHelper.gameObject.SetActive(false);
pullState = PullState.Idle;
throwObj.Reset(posThrownObj);
}
private readonly WaitForSeconds wtTimeBeforeClear = new WaitForSeconds(0.3F);
IEnumerator ClearTrail()
{
yield return wtTimeBeforeClear;
trail.Clear();
trail.enabled = false;
}
Vector3 velocity = Vector3.zero;
public Vector3 CalcVelocity(Vector3 diff, float distance)
{
velocity.x = diff.x * throwSpeed * distance * coofDotHelper;
velocity.y = diff.y * throwSpeed * distance * coofDotHelper;
return velocity;
}
}
}
Edit your code bro and paste only where your function is.
Answer by corteg10 · Dec 08, 2017 at 04:04 AM
It's a bit to long to read but it sounds like you will need to add a boolean to store whether or not the ball has been thrown.
If the ball has been thrown, don't let the code that throws it execute by wrapping it around an if statement (that only runs if it hasn't been thrown).
Private bool isThrown = false;
if (! isThrown) { -Code to throw ball- isThrown= true; }
Something like that should work. Good luck!
Answer by Ginxx009 · Dec 08, 2017 at 04:13 AM
It's a bit boring to read those code like @cortego10 cause its too long but the logic behind that could be something like this too.
private bool isThrown = false;
void FunctionOfThrowingTheBall(){
//... code for throwing the ball
isThrown = true;
}
void FunctionDontThrowBall(){
//..code for not throwing the ball
}
// just add the getKey function for pressing the R.
void Update(){
if(isThrown){
FunctionDontThrowBall();
} else {
FunctionOfThrowingTheBall();
}
}
Your answer
Follow this Question
Related Questions
Issue with adding force and lerping position? Possible unity bug? 0 Answers
On Collision Enter Method Stuck With Slow Motion Time 1 Answer
Multiple Cars not working 1 Answer
How to create a variable jump height 2 Answers
How can I make sure two movable objects stay a certain distance away from eachother? 0 Answers