I’ve been trying to make a simple pinball game. The table and flippers went OK, but now I’m trying to add a “spring” that will launch the ball onto the table.
I’ve been using this video
as a guide for most of it. The guy is using javascript and I’m using C#, but I’ve been able to manage so far. But I’ve tried translating his spring script and it just isn’t working no matter how hard I try.
His code:
#pragma strict
var inputButtonName : String = "Pull";
var distance : float = 50;
var speed : float = 1;
var ball : GameObject;
var power : float = 2000;
private var ready : boolean = false;
private var fire : boolean = false;
private var moveCount : float = 0;
function OnCollisionEnter(_other : Collision) {
if(_other.gameObject.tag == "Ball"){
ready = true;
}
}
function Update () {
if(Input.GetButton(inputButtonName)){
//As the button is held down, slowly move the piece
if(moveCount < distance){
transform.Translate(0,0,-speed * Time.deltaTime);
moveCount += speed * Time.deltaTime;
fire = true;
}
}
else if(moveCount > 0){
//Shoot the ball
if(fire && ready){
ball.transform.TransformDirection(Vector3.forward * 10);
ball.rigidbody.AddForce(0, 0, moveCount * power);
fire = false;
ready = false;
}
//Once we have reached the starting position fire off!
transform.Translate(0,0,20 * Time.deltaTime);
moveCount -= 20 * Time.deltaTime;
}
//Just ensure we don't go past the end
if(moveCount <= 0){
fire = false;
moveCount = 0;
}
}
My code (“up” in my game is negative on the x axis, while in his it’s positive on the z axis. I’ve also added some debug logs that tell me when a certain stage is triggered):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Plunger : MonoBehaviour
{
public string launch = "launch";
public float distance = 50f;
public float speed = 1f;
public float power = 2000f;
public GameObject ball;
bool fire = false;
public float moveCount = 0;
bool ready = false;
// Start is called before the first frame update
void Start()
{
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Ball")
{
ready = true;
Debug.Log("ready");
}
}
// Update is called once per frame
void Update()
{
if (Input.GetButton(launch))
{
if (moveCount < distance)
{
transform.Translate(speed * Time.deltaTime, 0, 0);
moveCount += speed * Time.deltaTime;
fire = true;
Debug.Log("fire");
}
else if (moveCount > 0)
{
if (fire && ready)
{
ball.transform.TransformDirection(Vector3.left * 10);
ball.GetComponent<Rigidbody>().AddForce(moveCount * power, 0, 0);
fire = false;
Debug.Log("shoot");
}
transform.Translate(-50 * Time.deltaTime, 0, 0);
moveCount -= 50 * Time.deltaTime;
}
if (moveCount <= 0)
{
fire = false;
moveCount = 0;
}
}
}
}
The ball doesn’t shoot up and the spring starts going down properly, but then starts jumping up and down if you keep holding the button.
At this point I’d be fine even with just a way to make the ball shoot up by pressing a button, so if someone knows how to do that I’ll be grateful for that as well.
Picture of the board (white thing on the right is the spring)