Hello there. I am pretty new to Unity, but have had some success in making my first game (Pong). Now I've decided to spice up the gameplay a little by allowing each player (the two paddles are controlled using Input.GetAxis) a variety of powerups that will affect the other player. I am having problems stopping the rigidbody (paddle) from moving at the moment. I have tried movement constraints, making the object kinematic but nothing seems to work. The goal here is that upon pressing a key, the paddle will stop moving temporarily, and then move again as usual. Here's the movement code:
var ball : GameObject;
var keyboardSpeed = 20;
var slowtime = 2;
function FixedUpdate() {
var yMove = Input.GetAxis("Vertical2") * keyboardSpeed * Time.deltaTime ;
transform.Translate(Vector3(0,yMove,0));
transform.position.y = Mathf.Clamp(transform.position.y, -9, 9);
var newPos = rigidbody.position + Vector3(0, yMove, 0);
if (Input.GetKey ("d")) {
Slow ();
}
// Move the paddle
rigidbody.MovePosition(newPos);
}
function Slow () {
keyboardSpeed = 1;
yield WaitForSeconds(slowtime);
keyboardSpeed = 20;
}
function OnCollisionEnter (collision : Collision) {
if (collision.gameObject == ball) {
collision.rigidbody.AddForce (20,0,0);
}
}
// Require a Rigidbody component to be attached to the same GameObject.
@script RequireComponent(Rigidbody)
I have had success in slowing down the paddle temporarily (function Slow), but no joy with stopping the paddle. Any help and advice (to this issue or my bad scripting) would be very much appreciated.