-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerController.cs
More file actions
68 lines (50 loc) · 1.94 KB
/
Copy pathPlayerController.cs
File metadata and controls
68 lines (50 loc) · 1.94 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float horizontalInput;
public float speed = 10.0f;
public GameObject projectilePrefab;
//Below method can be used to mimite player's x axis moment
// public float leftBoundry = -10.0f;
//public float rightBoundry = 10.0f;
//But we are using this method
public float xrange = 10.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//Bwlow method can be used to mimite player's x axis moment
// if (transform.position.x < -10)
// {
// transform.position = new Vector3(leftBoundry, transform.position.y, transform.position.z);
// }
//Bwlow method can be used to mimite player's x axis moment
// if(transform.position.x > 10)
// {
// transform.position = new Vector3(rightBoundry, transform.position.y, transform.position.z);
// }
//But we are using this method
//Keep the player in bound
if (transform.position.x < -xrange)
{
transform.position = new Vector3(-xrange, transform.position.y, transform.position.z);
}
if (transform.position.x > xrange)
{
transform.position = new Vector3(xrange, transform.position.y, transform.position.z);
}
horizontalInput = Input.GetAxis("Horizontal");
transform.Translate(Vector3.right * horizontalInput * speed * Time.deltaTime);
//Get the input key Space bar from the user
if (Input.GetKeyDown(KeyCode.Space))
{
//Launch a projectile from the player
Instantiate(projectilePrefab, transform.position, projectilePrefab.transform.rotation);
}
}
}