-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerMovement.cs
59 lines (49 loc) · 1.61 KB
/
PlayerMovement.cs
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(Rigidbody2D))]
public class PlayerMovement : MonoBehaviour
{
//necessary for animations and physics
private Rigidbody2D rb2D;
private Animator myAnimator;
private bool facingRight = true;
//variables to play with
// speed gets overridden by the speed variable setting in the player object script component
public float speed = 2.0f;
public float horizMovement; //= 1[OR]-1[OR]0
// Start is called before the first frame update
private void Start()
{
//define the gameobjects found on the player
rb2D = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
}
// Update is called once per frame
//handles the input for physics
private void Update()
{
//check direction given by player
horizMovement = Input.GetAxis("Horizontal");
}
//handles running the physics
private void FixedUpdate()
{
//move the character left and right
rb2D.velocity = new Vector2(horizMovement*speed,rb2D.velocity.y);
Flip(horizMovement);
myAnimator.SetFloat("speed", Mathf.Abs(horizMovement));
}
//flipping function /Moss
private void Flip (float horizontal)
{
if (horizontal < 0 && facingRight || horizontal > 0 && !facingRight)
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
}