I’ve tried this tutorial multiple times and I can’t see to see what I’m missing. Here is a link to my attempt to write the Pong code. You can possibly catch my error by viewing the code:
( ball.cs )
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour {
public float ballVelocity = 3000;
Rigidbody rb;
bool isPlay;
int randInt;
// Use this for initialization
void Awake ()
{
rb = gameObject.GetComponent<Rigidbody>();
randInt = Random.Range(1,3);
}
// Update is called once per frame
void Update ()
{
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));
}
}
}
}
… and (paddle.cs) …
using UnityEngine;
using System.Collections;
public class Paddle : MonoBehaviour
{
public float paddleSpeed = 1F;
public Vector3 playerPos = new Vector3(0,0,0);
void Update ()
{
float yPos = gameObject.transform.position.y + (Input.GetAxis("Vertical") * paddleSpeed)/2;
playerPos = new Vector3 (-20,Mathf.Clamp(yPos,-14,14),0);
gameObject.transform.position = playerPos;
}
}
Can anyone catch my error?