I have no idea how to create a script for my character object!

Hey y’all.

I’m a beginner who doesn’t know the head & tail of setting up a script that can get the cylinder standing in as my 3d protagonist to move forward, backward, left right and rotate so she can turn around and look forward.

It’s a survival horror game in the style of outlast so she’s gotta look everywhere.

I have no idea how to begin a script or how to assign the values and functions correctly. I have so many reds in my console.

I’ve been at this for four hours ;_; pls help!

That sounds like a cool project but perhaps take a break from it and check out some of the basic tutorials if you are just getting started. I would recommend the pathways as a great place to start if you are just getting started with Unity. There is also a path for specifically for programming if its scripting which is giving you the most challenges:

You are welcome! Have fun

thanks so much, i gotchu!

It happens that I’m working on a player Controller Script so this might be helpful, you can move, jump and sprint( if you release sprint key it regenerates )

Setup: PlayerMovement

  1. Create a new Script on your Player , call it PlayerMovement to match the script name , open it and paste this script below , i would suggest you try and learn from it

  2. save and return to Unity , when done compiling all you need to do is create a GameObject under your player called groundCheck and position it near the bottom of the player if your player is a silinder vir 1:1:1 dimensions set the Transform to (0, -0.9, 0).

  3. Now lastly you need to create a new LayerMask for the ground, In the top right of the inspector you’ll see Layer : Default" click on it and Add a new layer called Ground.

  4. On your Player script there is a field called GroundMask change it to the newly created "Ground" layer, Also select all your Ground GameObjects which you want to be able to jump on and set their Layer to ground.

It should work now , for more info watch the Brackeys youtube video on youtube about settings up fpsMovement, which this script is based on.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(CharacterController))]
public class PlayerMovement : MonoBehaviour
{
    [SerializeField] InputManager inputManager;
    
    [SerializeField] private float walkSpeed = 6f;
    [SerializeField] private float sprintSpeed = 9f;
    [SerializeField] private float jumpHeight = 1f;

    //Sprinting
    private bool isSprinting;
    private float stamina = 100f;
    [SerializeField] private float staminaDepletionRate = 50f;
    [SerializeField] private float regenerationDelay = 1.5f;
    [SerializeField] private float regenerationRate = 30f;

    //Ground Detection
    private bool isGrounded;
    [SerializeField] private Transform groundCheck;
    [SerializeField] private LayerMask groundMask;
    [SerializeField] private float groundDistance = 0.4f;

    //Physics
    private Vector3 velocity;
    public float gravity = -9.81f;
    
    private CharacterController controller;

    private void Start()
    {
        controller = GetComponent<CharacterController>();
    }

    private void Update()
    {
        //GroundCheck
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        //Defining Axis's
        float moveX = Input.GetAxisRaw("Horizontal");
        float moveZ = Input.GetAxisRaw("Vertical");

        Vector3 moveDirection = transform.right * moveX + transform.forward * moveZ;

        //Applying Velocity to controller
        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);

        //Jumping
        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }

        //Sprinting

        if (Input.GetKey(KeyCode.LeftShift) && stamina >= 2)
        {
            controller.Move(moveDirection * sprintSpeed * Time.deltaTime);
            Sprint();
        }
        else
        {
            controller.Move(moveDirection * walkSpeed * Time.deltaTime);
            StartCoroutine(StopSprint());
        }

        //Clamp stamina between 0f and 100f
        stamina = Mathf.Clamp(stamina, 0f, 100f);
    }

    private void Sprint()
    {
        isSprinting = true;
        stamina -= staminaDepletionRate * Time.deltaTime;
    }

    private IEnumerator StopSprint()
    {
        isSprinting = false;
        yield return new WaitForSeconds(regenerationDelay);
        stamina += regenerationRate * Time.deltaTime;
    }

}

Setup: MouseLook

1.Add the script below to your player Camera and Reference your player.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseLook : MonoBehaviour
{
    public float mouseSensitivity = 100f;
    public Transform playerBody;
    float xRotation = 0f;

    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
        float MouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
        float MouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
        

        xRotation -= MouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        playerBody.Rotate(Vector3.up * MouseX);
        
    }
}

That’s all hope this helps.

so the mouse script for the camera is finna allow my player to look around??? i only just saw this, thanks sm.

oh omg, my console keeps bringing up toplevel statements ;___; should have done some c# exercises before i did this LMAO

Yeah its still saying the thing about toplevel statements and I’m not sure what you mean by “reference your player”. LOL sorry for my incompetence