Hi guys
I’m making a Foot Golf type game.
I have a joystick and a button
When you keep the button pressed the power builds up and then the ball gets hit accordingly
direction left and right also works with the joystick. but its far from perfect. The ball if just hit along the ground hardly rolls and i cant seem to get a nice lob effect. ie lower power, higher angle.
lastly, are terrain colliders a bit flakey? as sometimes my ball just disappears and i have friction on the ball too but it never seems to stop rolling, or it hardly rolls when i put friction up.
Was looking on youtube for some type of tutorial but didnt find anything suitable. Wondered if somebody on here that understands these types of dynamics can give me a hand.
If you need me to post videos i can do that too.
here is my code this far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SoccerPlayerController : MonoBehaviour
{
public AudioSource ballKickSound;
public float movX;
public float movY;
public GameObject ball;
public Rigidbody ballRB;
public float ballSpeed;
public Animator ani;
private bool kickButtonHeldDown;
public float kickPower;
public Text kickPowerIndicator;
float maxKickPower = 2000;
float leftRightMagnitude;
float upDownMagnitude;
float minKickPower = 500;
float kickChargeSpeed = 1200f;
private bool canKickBall = true;
private void Update()
{
CheckKickPower();
CheckBallMovement();
}
public void CheckKickPower()
{
if (kickButtonHeldDown && kickPower <= maxKickPower)
{
if (kickPower <= minKickPower)
{
kickPower += Time.deltaTime * kickChargeSpeed + 720;
}
else
{
kickPower += Time.deltaTime * kickChargeSpeed;
if (canKickBall && kickButtonHeldDown)
{
float convertPowerToNumber = kickPower / 140f;
//convertPowerToNumber = Mathf.Floor(convertPowerToNumber);
convertPowerToNumber -= 4;
int canonPower = (int)convertPowerToNumber;
kickPowerIndicator.text = canonPower.ToString();
}
}
}
}
public void KickButtonDown()
{
kickButtonHeldDown = true;
}
public void KickButtonReleased()
{
kickButtonHeldDown = false;
KickBall(kickPower);
kickPower = 0;
}
public void KickBall(float shotPower)
{
print("in kickball = " + shotPower);
if (shotPower < 1300)
{
ani.SetBool("MediumKick", true);
}
else
{
ani.SetBool("HardKick", true);
}
StartCoroutine(KickDelay(shotPower));
//ballRB.AddForce(0, shotPower * 2 * Time.deltaTime, 0);
}
void CheckBallMovement()
{
movX = SimpleInput.GetAxis("Horizontal");
leftRightMagnitude = movX * 1500;
movY = SimpleInput.GetAxis("Vertical");
upDownMagnitude = movY * 1000;
}
IEnumerator KickDelay(float shotPower)
{
float waiting = 0.8f;
yield return new WaitForSeconds(waiting);
ballKickSound.Play();
ballRB.AddForce(0, 0, shotPower * 2 * Time.deltaTime);
ballRB.AddForce(0, upDownMagnitude * 2 * Time.deltaTime, 0);
ballRB.AddForce(leftRightMagnitude * 2 * Time.deltaTime, 0, 0);
StartCoroutine(ReloadScene());
}
IEnumerator ReloadScene()
{
float waiting = 6f;
yield return new WaitForSeconds(waiting);
SceneManager.LoadScene("Hole1");
}
}