Question by
chalogr · Aug 24, 2016 at 05:31 PM ·
scripting problemscripting beginner
Need help increasing GameObject speed without affecting direction OnCollision.
Hello,
I have been trying to add gameplay features to my recently created Pong clone. I wanted my ball to increase in speed every time it bounced (entered collision with another object). I haven't been able to figure this out. I can only get to the RigidBody's velocity, but that is not what I want, since tinkering with this also has some sort of effect on my ball's direction. Here is my code;
using UnityEngine;
using System.Collections;
public class BallScript : MonoBehaviour {
public float ballVelocity = 3000;
public float thrust = 500;
Rigidbody rb;
bool isPlay;
int randInt;
void Awake () {
rb = gameObject.GetComponent<Rigidbody>();
randInt = Random.Range(1,3);
}
void FixedUpdate () {
if (Input.GetMouseButton(0)==true && isPlay == false) {
transform.parent = null;
isPlay = true;
rb.isKinematic = false;
if(randInt==1){
rb.AddForce (new Vector3 (ballVelocity, ballVelocity, 0));
}
if(randInt==2){
rb.AddForce (new Vector3 (ballVelocity, -ballVelocity, 0));
}
}
}
void OnCollisionEnter(Collision collision) {
rb.AddForce (transform.forward * thrust);
}
}
As you can see, I have tried increasing the ball's speed by using "rb.AddForce (transform.forward * thrust);", however, this actually does nothing and the ball keeps moving at the same speed even after hitting other colliders. What am I missing here? Please help, this is just my second week using the program.
Comment