HI, I would like that the ball increases its velocity every time the score id a multiple of 10, I tried the code below, but it only works the first time…
public Rigidbody rb;
private float forza = 305f;
public bool partito;
public bool gameOver;
public static MovimentoPalla current;
float i;
private void Awake()
{
if(current== null)
current = this;
}
// Start is called before the first frame update
void Start()
{
i = 0f;
partito = false;
gameOver = false;
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (gameObject != null)
{
if (partito && !gameOver)
{
UIManager.current.GameStart();
}
}
}
private void FixedUpdate()
{
if (!partito)
{
if (Input.GetMouseButtonDown(0))
{
partito = true;
}
if (partito && !gameOver)
{
if (ScoreScript.current.scoreValue % 10 == 0)
{
i += 5f;
forza = forza + i;
}
rb.velocity = new Vector3(0, 0, forza * Time.deltaTime);
}
}
if (gameOver)
rb.velocity = Vector3.zero;
}
}
Because you replace current value with the same value in every fixed update. This is what line 49 in your code commands the machine to do: set rigidbody velociy to be x=0,y=0, and z = forza;
If you want to inscrease it, you need to use addition instead of assignment. Tell the machine to add forza to current velocity of the body instead.
In real life objects move smoothly because their resulting velocities are determined by applied forces, you can’t just change some random object velocity and expect the resulting motion to look smooth.
If you stand at 0km/h, then at sudden run at 15km/h, then at sudden walk 6km/h, of course it won’t be smooth.
Changing velocity, is just like driving a car and at sudden hitting a wall.
What you should have to smooth out, is to implement acceleration.
Or Lerp velocity over time.
public Rigidbody rb;
public float forza;
public bool partito;
public bool gameOver;
public float forzaSeSecondaLife;
public static MovimentoPalla current;
public float time= 0.0f;
int i;
private void Awake()
{
if(current== null)
current = this;
}
// Start is called before the first frame update
void Start()
{
forza = 300f;
i = 0;
partito = false;
gameOver = false;
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (gameObject != null)
{
if (Input.GetMouseButtonDown(0) && !partito)
partito = true;
if (Input.GetMouseButtonDown(0) && partito && !gameOver)
{
if (i == 0)
{
FindObjectOfType<AudioManager>().Play("RollingBall");
i = 1;
}
UIManager.current.GameStart();
}
}
}
private void FixedUpdate()
{
if (partito && !gameOver)
{
[QUOTE="Antypodish, post: 5221166, member: 616942"]Ok.
But how did you command ball to move before then?
Did you also used follwoing, with fixed forza?
[code=CSharp]rb.velocity = new Vector3(0, 0, forza * Time.deltaTime) ;
[/QUOTE]
rb.velocity = new Vector3(0, 0, forza * Time.fixedDeltaTime);
}
if (gameOver)
rb.velocity = Vector3.zero;
}[/code]
The code after:
public Rigidbody rb;
public float forza;
public bool partito;
public bool gameOver;
public float forzaSeSecondaLife;
public static MovimentoPalla current;
public float time= 0.0f;
int i;
private void Awake()
{
if(current== null)
current = this;
}
// Start is called before the first frame update
void Start()
{
forza = 300f;
i = 0;
partito = false;
gameOver = false;
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (gameObject != null)
{
if (Input.GetMouseButtonDown(0) && !partito)
partito = true;
if (Input.GetMouseButtonDown(0) && partito && !gameOver)
{
if (i == 0)
{
FindObjectOfType<AudioManager>().Play("RollingBall");
i = 1;
}
UIManager.current.GameStart();
}
}
}
private void FixedUpdate()
{
if (partito && !gameOver)
{
time = time + Time.fixedDeltaTime;
if (time > 1f)
{
forza += 2.5f;
time = 0.0f;
}
rb.velocity = new Vector3(0, 0, forza * Time.fixedDeltaTime);
}
if (gameOver)
rb.velocity = Vector3.zero;
}
Remove/comment out everything in FixedUpdate.
Add random force at Start.
Or, drop it on the slope, so ball can accelerate, then make horizontal plane, to allow ball roll freely on the flat ground.
See if it rolls correctly.