-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerController.cs
107 lines (91 loc) · 3.16 KB
/
PlayerController.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
105
106
107
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
private Rigidbody2D rb;
private Vector2 targetPosition;
private bool isMoving;
public bool isSelected;
private SpriteRenderer spriteRenderer;
public Color selectedColor = Color.red; // Color when selected
private Color originalColor; // Original color of the player
private void Start()
{
// Add Rigidbody2D component if not already present
rb = gameObject.GetComponent<Rigidbody2D>();
if (rb == null)
{
rb = gameObject.AddComponent<Rigidbody2D>();
}
// Configure Rigidbody2D
rb.gravityScale = 0; // No gravity
rb.constraints = RigidbodyConstraints2D.FreezeRotation; // Freeze Z rotation
// Initialize other components
spriteRenderer = GetComponent<SpriteRenderer>();
originalColor = spriteRenderer.color; // Store the original color
targetPosition = rb.position; // Initialize targetPosition with the player's current position
}
private void Update()
{
mouseLeftClick();
mouseRightClick();
}
private void mouseLeftClick()
{
if (Input.GetMouseButtonDown(0)) // 0 is the left mouse button
{
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos2D = new Vector2(mousePosition.x, mousePosition.y);
RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
if (hit.collider != null && hit.collider.gameObject == gameObject)
{
isSelected = true;
spriteRenderer.color = selectedColor; // Change color when selected
Debug.Log("Player selected!");
}
else if (isSelected)
{
targetPosition = new Vector2(mousePosition.x, mousePosition.y);
isMoving = true;
}
}
}
// Method to handle right-click for deselection
private void mouseRightClick()
{
if (Input.GetMouseButtonDown(1))
{
isSelected = false;
spriteRenderer.color = originalColor; // Revert to original color when deselected
}
}
private void FixedUpdate()
{
if (isMoving)
{
Vector2 newPosition = Vector2.MoveTowards(rb.position, targetPosition, moveSpeed * Time.fixedDeltaTime);
rb.MovePosition(newPosition);
// Check if the player has reached the target position
if (Vector2.Distance(rb.position, targetPosition) < 0.1f)
{
isMoving = false;
}
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
// Handle collision with other objects
if (collision.gameObject.CompareTag("Obstacle"))
{
Debug.Log("Player collided with an obstacle!");
}
}
private void OnTriggerEnter2D(Collider2D other)
{
// Handle trigger collisions with other objects
if (other.gameObject.CompareTag("Item"))
{
Debug.Log("Player picked up an item!");
}
}
}