Hi all, have you tried ChatGPT?

Have been playing around with it for a little while now, its simply amazing.
Apart from that it can wite stories, poems, explain science, and so much more;
it can also write Unity Scripts. Ofc you still need your own knowledge and how to implement it,
but its just so cool.

For example i asked this;

Can you write me a unity wasd movement script, with jump on spacebar and left clicking the mouse will fire a projectile at the mouse cursor

Then it generated this;

using System.Collections;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 10.0f;
    public float jumpForce = 10.0f;
    public GameObject projectilePrefab;

    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(horizontal, 0, vertical);

        rb.AddForce(movement * speed);

        if (Input.GetButtonDown("Jump"))
        {
            rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
        }

        if (Input.GetMouseButtonDown(0))
        {
            ShootProjectile();
        }
    }

    void ShootProjectile()
    {
        Vector3 mousePos = Input.mousePosition;
        mousePos = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, Camera.main.transform.position.y));

        Vector3 direction = (mousePos - transform.position).normalized;

        GameObject projectile = Instantiate(projectilePrefab, transform.position, Quaternion.identity);
        Rigidbody projectileRb = projectile.GetComponent<Rigidbody>();

        projectileRb.AddForce(direction * speed, ForceMode.Impulse);
    }
}

and it even put this explanation at the end;

This script should be attached to the player game object in the scene. The projectilePrefab is a reference to the prefab of the projectile that will be fired. The speed variable controls the movement speed of the player and the jumpForce variable controls the force applied to the player when jumping.

Note that this script is just a starting point, and you may need to make changes to suit your specific requirements.

Privacy & Terms