Skip to content

Instantly share code, notes, and snippets.

@MarcScott
Created March 13, 2023 09:28
Show Gist options
  • Save MarcScott/a61f936b30ab9e12864b4d7005f61128 to your computer and use it in GitHub Desktop.
Save MarcScott/a61f936b30ab9e12864b4d7005f61128 to your computer and use it in GitHub Desktop.
Ball Controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallController : MonoBehaviour
{
private Rigidbody rb;
public Transform cameraTransform;
// Start is called before the first frame update
void Start()
{
rb = this.GetComponent<Rigidbody>();
rb.transform.forward = cameraTransform.forward;
}
void Update()
{
if (Input.GetKeyDown("space"))
{
rb.AddForce(0f, 75f, 0f);
}
}
// FixedUpdate is called once per fixed frame-rate frame
void FixedUpdate()
{
// Calculates cameraTransform.forward without the y value so the ball doesn't move up and down on the Y axis
Vector3 forward = new Vector3(cameraTransform.forward.x, 0, cameraTransform.forward.z).normalized;
Vector3 right = Quaternion.AngleAxis(90, Vector3.up) * forward;
Vector3 left = -right;
Vector3 backward = -forward;
if (Input.GetKey("d"))
{
rb.AddForce(right * 5f);
}
if (Input.GetKey("a"))
{
rb.AddForce(left * 5f);
}
if (Input.GetKey("w"))
{
rb.AddForce(forward * 10f);
}
if (Input.GetKey("s"))
{
rb.AddForce(backward * 2f);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment