- Home /
Rigidbody Problems
I am making a 2D golf game where you drag back and shoot forwards. My problem is that when I am playing it, I shoot and the ball goes really fast, but sometimes it goes really slowly.
This is the script:
if (this.GetComponent<Rigidbody2D>().velocity.magnitude <= 0.1f && inHole == false)
{
ready = true;
} else
{
ready = false;
//Debug.Log(this.GetComponent<Rigidbody2D>().velocity.magnitude.ToString());
}
if (Input.GetMouseButtonDown(0) == true && !aiming && ready)
{
aiming = true;
}
if (targetGoal != null)
{
this.transform.position = Vector2.Lerp(this.transform.position, targetGoal.transform.position, 2f);
}
if (aiming)
{
prevPos.transform.position = this.transform.position;
//ENABLE LINE
line.GetComponent<LineRenderer>().enabled = true;
//CONTROL INPUT && SHOOT POSITIONS
startPos = this.transform.position;
line.GetComponent<LineRenderer>().SetPosition(0, startPos);
Vector3 shootPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
shootPos.z = 0;
shootPos = this.transform.position + (this.transform.position - shootPos);
endPos = shootPos;
if (Vector3.Distance(startPos, shootPos) > maxDist)
{
Vector3 dir = endPos - startPos;
endPos = this.transform.position + (dir.normalized * maxDist);
}
line.GetComponent<LineRenderer>().SetPosition(1, endPos);
} else
{
line.GetComponent<LineRenderer>().enabled = false;
}
if (Input.GetMouseButtonUp(0) == true && aiming && ready)
{
endPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Shoot();
}
private void Shoot()
{
aiming = false;
//Debug.Log("Shooting @" + endPos.ToString());
if(Vector2.Distance(startPos, endPos) > maxDist)
{
}
Vector2 direction = startPos - endPos;
this.GetComponent<Rigidbody2D>().AddForce(direction * speed * Time.deltaTime);
score++;
overallScore++;
overallScoreKeeper.text = overallScore.ToString();
scoreKeeper.text = score.ToString();
}
Any help would be appreciated. Thanks.
Answer by MaxLevelNoob · Apr 23, 2020 at 03:46 PM
Everytime you're trying to get the direction of a Vector, you want to make sure it's normalized. If you do not normalize it, then the magnitude of the Vector will vary.
private void Shoot()
{
aiming = false;
//Debug.Log("Shooting @" + endPos.ToString());
if(Vector2.Distance(startPos, endPos) > maxDist)
{
}
Vector2 direction = (startPos - endPos).normalized;
this.GetComponent<Rigidbody2D>().AddForce(direction * speed * Time.deltaTime);
score++;
overallScore++;
overallScoreKeeper.text = overallScore.ToString();
scoreKeeper.text = score.ToString();
No problem, stay safe and happy developing :)
@$$anonymous$$axLevelNoob I have tested this and have realized that the speed of the ball is fast no matter what. even if I drag back just a little.
Your answer
Follow this Question
Related Questions
My player jumps higher when the ground is aslant? 1 Answer
Object hits another but doesnt bounce off again. 0 Answers
Rigidbody2D Problems With Golf Game 0 Answers
Jumping problems (how to stop a force) 1 Answer
There is no 'Rigidbody' attached 1 Answer