Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C# by hbarcus ( 7 years ago )
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Paddle : MonoBehaviour
{
    Rigidbody rb;
    bool bLeftReq, bRightReq;
    float fSpeed = 15f;
    Vector3 vDirModifier;

    Ball b;
    Vector3 vBallOffset;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.mass = 1000;
        rb.useGravity = false;
        rb.constraints =
            RigidbodyConstraints.FreezePositionY |
            RigidbodyConstraints.FreezePositionZ |
            RigidbodyConstraints.FreezeRotation;

        b = FindObjectOfType<Ball>();

        vBallOffset.y = GetComponent<Renderer>().bounds.extents.y + b.GetComponent<Renderer>().bounds.extents.y;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
        {
            bLeftReq = true;
        }

        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
        {
            bRightReq = true;
        }

        if (!GameManager.instance.Playing)
        {
            b.transform.position = this.transform.position + vBallOffset;

            if (Input.GetKeyDown(KeyCode.Space))
            {
                GameManager.instance.Playing = true;
                b.Launch();
            }
        }
    }

    void FixedUpdate()
    {
        vDirModifier.x = 0;

        if (bLeftReq)
        {
            bLeftReq = false;
            vDirModifier.x -= fSpeed;
        }

        if (bRightReq)
        {
            bRightReq = false;
            vDirModifier.x += fSpeed;
        }

        rb.velocity = vDirModifier;
    }
}

 

Revise this Paste

Your Name: Code Language: