-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerMovement.cs
More file actions
86 lines (72 loc) · 2.3 KB
/
PlayerMovement.cs
File metadata and controls
86 lines (72 loc) · 2.3 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
private Rigidbody2D playerRigidBody2D;
private float movePlayerVector;
private bool facingRight;
public static bool canMove = true;
public bool isInteracting = false;
public float speed = 4.0f;
private GameObject playerSprite;
private Animator anim;
public Transform player;
private Vector3 playerCurrentPosition;
public Vector3 playerOriginalPosition;
public bool moving = false;
public AudioSource walk0;
private AudioSource currentSound;
void Awake()
{
// Setting up references.
playerRigidBody2D = (Rigidbody2D)GetComponent(typeof(Rigidbody2D));
playerSprite = transform.Find("PlayerSprite").gameObject;
anim = (Animator)playerSprite.GetComponent(typeof(Animator));
player = GameObject.Find ("Player").transform;
playerOriginalPosition = player.position;
if (GameState.LastScenePositions.ContainsKey("Town")) {
playerCurrentPosition = GameState.GetLastScenePosition("Town");
} else {
playerCurrentPosition = playerOriginalPosition;
}
currentSound = walk0;
player.position = playerCurrentPosition;
}
// Update is called once per frame
void Update()
{
// Cache the horizontal input.
if (canMove && !isInteracting) {
movePlayerVector = Input.GetAxis ("Horizontal");
anim.SetFloat ("speed", Mathf.Abs (movePlayerVector));
playerRigidBody2D.velocity = new Vector2 (movePlayerVector * speed, playerRigidBody2D.velocity.y);
if (playerRigidBody2D.velocity.x == 0)
moving = false;
else
moving = true;
if (movePlayerVector < 0 && !facingRight) {
Flip ();
} else if (movePlayerVector > 0 && facingRight) {
Flip ();
}
} else {
moving = false;
anim.SetFloat ("speed", 0f);
playerRigidBody2D.velocity = new Vector2 (0f,0f);
}
if (!currentSound.isPlaying && moving && anim.GetFloat("speed")>= 0.05f) {
float randomiser2 = Random.Range (0.3f, 0.4f);
currentSound.volume = randomiser2;
currentSound.PlayDelayed (0.5f);
}
}
void Flip()
{
// Switch the way the player is labelled as facing.
facingRight = !facingRight;
// Multiply the player's x local scale by -1.
Vector3 theScale = playerSprite.transform.localScale;
theScale.x *= -1;
playerSprite.transform.localScale = theScale;
}
}