Unity Tutorial: Roll A Ball - Part 2: Player & Camera Movement

preview_player
Показать описание
This tutorial is based off of Unity's classic "Roll A Ball" but it is in a shorter format so you can learn the same concepts in less time. It shows how to build a rolling ball game from scratch in Unity and C#. Concepts include player movement, Unity's new Input System, Collisions, Scoring, and UI with TextMeshPro text elements.

The code and concept comes from Unity's site. I am just presenting it a little differently
Рекомендации по теме
Комментарии
Автор


After this video your PlayerController class will look like this:


using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
public float speed = 10.0f;
private Rigidbody rb;

//holds movement x and y
private float movementX;
private float movementY;

// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>(); //sets rigidbody component to rb
}

void OnMove(InputValue movementValue)
{
//create a vector 2 variable and store the x and y movement values in it
Vector2 movementVector =
movementX = movementVector.x;
movementY = movementVector.y;
}

// Update is called once per frame
void FixedUpdate()
{
//set the movement to the x and z variables (keep y at 0)
Vector3 movement = new Vector3(movementX, 0.0f, movementY);
rb.AddForce(movement * speed);
}
}


and your CameraController class will look like this:



using UnityEngine;

public class CameraController : MonoBehaviour
{
public GameObject player; //will reference the player game object's position
private Vector3 offset; //will set the offset position from the player to the camera

// Start is called before the first frame update
void Start()
{
//calculate the offset position between the camera and the player at the start of the game
//it subtracts the player's position from the camera's
offset = transform.position ­ player.transform.position;
}

// Update is called once per frame
void Update()
{
//sets the camera to where the player is plus the offset set above
transform.position = player.transform.position + offset;
}
}

Digestible
Автор

Mine just said it had a compiler error

ZenoVoids
Автор

When i put in the script the player still didn't move

-VORTEX-VR
Автор

How to install visual studio. That has opened when u create c#(5:10)?

sanjaykrishna
Автор

Hey I’m back and I have a question do you know how to make a rotatable cam? I know it’s possible but I need it because I’m planning on making a racing ball game and I need a rotatable cam just a question and if you don’t know how to that’s okay

Cosmitm
Автор

For me the compiler errors were
Vector2 could not be found 2 of these were not found
And
The name ‘movement’ does not exist in the current context

Cosmitm
Автор

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

public class playercontroller : MonoBehaviour
{

private Rigidbody rb;

//holds movement x and y
private float movementX;
private float movementY;

// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>(); //sets rigidbody component to rb
}

void OnMove(InputValue movementValue)
{
//create a Vecter 2 veriable and store the x and y movement values in it
Vecter2 moveVecter = movementValue. Get<vecter2>();

movementX = movement.X;
movementX = movement.y;

}

// Update is called once per frame
void FixwdUpdate()
{
//set the movement to the x and z variables (keep y at 0)
Vecter3 movement = new Vecter3(movementX, 0.0f, movementY);
rb.addforce(movement);

}
}
code at the time of putting this in it is kinda of broken but she uploaded a video on how to fix it

Cosmitm