I’m a newcomer to game development and programming. I’m having trouble trying to increase the speed of a ball using rigidbody2D. I saw the speed variable increases in the inspector-view but in game-view the speed stays the same. Please see my code below. Could you tell me what’s wrong? Thanks in advance
using UnityEngine;
using System.Collections;
public class BallControl : MonoBehaviour
{
public float speed = 2.0f;
Vector2 v2;
void Start ()
{
//random direction at the start
float x = Random.Range(-10.0f,10.0f);
float y = Random.Range(-10.0f,10.0f);
v2 = new Vector2 (x, y);
rigidbody2D.velocity = v2.normalized * speed;
}
void FixedUpdate()
{
//increment the speed
speed++;
}
}
Like vakabaka said, your speed is only set once inside the Start function, which is only called once. Your variable increases each update with FixedUpdate but you’re not increasing the rigidbodys velocity after the start function.
So move line 16 into your fixedupdate and it will work
Thanks for the replies. I’ve added in the new line in the script but the ball is acting weird now. The ball object has a physical material with bounciness of 1. Before the changes, the ball bounces off the collision wall. After adding in “rigidbody2D.velocity = v2.normalized * speed;” under “speed++”, the ball just stuck to the wall. Here’s a picture.