-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cs
More file actions
37 lines (30 loc) · 1.42 KB
/
Copy pathmain.cs
File metadata and controls
37 lines (30 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlsyerMovement : MonoBehaviour
{
Rigidbody rb;
[SerializeField] float playerSpeed = 5f; //Sets the float value for playerSpeed, SerializeField add this to the PlayerMovement componant in Unity.
[SerializeField] float jumpForce = 5f; //Sets the float value for jumpForce, SerializeField add this to the PlayerMovement componant in Unity.
// Start is called before the first frame update
void Start()
{
// Get the RigidBody from the player:
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
// Use the Unity preset Movement keys in 'Edit > Project Settings > Input Manager' and assigns the input (positve or negative value) to a variable.
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
//Sets the velocity of the player to the input value of Horizontal/Vertiacal * playerSpeed:
rb.velocity = new Vector3(horizontalInput * playerSpeed, rb.velocity.y, verticalInput * playerSpeed);
//Jump function for Spacebar, is Spacebar is pressed then...:
if(Input.GetButtonDown("Jump"))
{
//Set the players Y axis (height) to increase by the jumpForce:
rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
}
}
}