-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBobMovement.cs
104 lines (88 loc) · 2.76 KB
/
BobMovement.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
[SerializeField] Rigidbody2D rigid;
[SerializeField] float movement;
[SerializeField] public const int SPEED = 15;
[SerializeField] bool isFacingRight = true;
[SerializeField] const float JUMPFORCE = 600.0f;
[SerializeField] bool jumpPressed = false;
[SerializeField] bool isGrounded = true;
private Vector3 initialPosition;
public GameObject pinPrefab;
public float shootingSpeed = 10f;
// Start is called before the first frame update
void Start()
{
if (rigid == null)
rigid = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
movement = Input.GetAxis("Horizontal");
if (Input.GetButtonDown("Jump"))
jumpPressed = true;
if(Input.GetButtonDown("Fire1"))
{
ShootPin();
}
}
//can be called potentially many times per frame -- best for physics
void FixedUpdate()
{
rigid.velocity = new Vector2 (movement * SPEED, rigid.velocity.y);
if (movement < 0 && isFacingRight || movement > 0 && !isFacingRight)
{
Flip();
}
if (jumpPressed && isGrounded)
Jump();
}
void Flip()
{
transform.Rotate(0, 180, 0);
isFacingRight = !isFacingRight;
}
void Jump()
{
rigid.velocity = new Vector2 (rigid.velocity.x, 0);
rigid.AddForce(new Vector2 (0, JUMPFORCE));
jumpPressed = false;
isGrounded = false;
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
{
isGrounded = true;
jumpPressed = false;
}
}
void ShootPin()
{
Vector3 playerPosition = transform.position; // Get the player's position
GameObject pin = Instantiate(pinPrefab, playerPosition, Quaternion.identity);
// Calculate the shooting direction based on player's facing direction
Vector3 shootingDirection = isFacingRight ? Vector3.right : Vector3.left;
// Get the PinMovement script and set the shooting direction
PinMovement pinMovement = pin.GetComponent<PinMovement>();
if (pinMovement != null)
{
pinMovement.SetShootDirection(shootingDirection);
}
}
public void Respawn()
{
transform.position = initialPosition;
}
// void OnTriggerEnter2D(Collider2D other)
// {
// if (other.CompareTag("Balloon"))
// {
// Respawn();
// }
// }
}