-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayermovement
More file actions
80 lines (68 loc) · 2.28 KB
/
Playermovement
File metadata and controls
80 lines (68 loc) · 2.28 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
using UnityEngine;
using System.Collections;
public class Playermovement : MonoBehaviour
{
private float movementSpeed = 5.0f;
private bool isGrounded = true;
private bool hasDouble = true;
private float tempMoveSpeed;
public int jumpForce = 300;
void Update()
{
GetComponent<Rigidbody>().velocity = new Vector3(0, GetComponent<Rigidbody>().velocity.y, 0); //Set X and Z velocity to 09
//Vector3 movement = (Input.GetAxis("Horizontal") * -Vector3.left * movementSpeed) + (Input.GetAxis("Vertical") * Vector3.forward * movementSpeed);
//movement *= Time.deltaTime;
float moveInputX = (Input.GetAxis("Horizontal") * Time.deltaTime * movementSpeed);
//float moveInputZ = (Input.GetAxis("Vertical") * Time.deltaTime * movementSpeed);
transform.position += new Vector3(moveInputX, 0, 0); //last 0 is moveInputZ if needed
if (Input.GetButtonDown("Jump"))
{
if (isGrounded)
{
GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
GetComponent<Rigidbody>().AddForce(new Vector3(0, jumpForce, 0), ForceMode.Force);
isGrounded = false;
movementSpeed = 10;
hasDouble = true;
}
else if (hasDouble)
{
GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
GetComponent<Rigidbody>().AddForce(new Vector3(0, jumpForce, 0), ForceMode.Force);
isGrounded = false;
hasDouble = false;
movementSpeed = 10;
}
}
while (Input.GetKeyDown(""))
{
tempMoveSpeed = movementSpeed;
movementSpeed = movementSpeed + 5;
}
movementSpeed = tempMoveSpeed;
}
void Jump()
{
if (!isGrounded) {return;}
isGrounded = false;
GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
GetComponent<Rigidbody>().AddForce(new Vector3(0, 300, 0), ForceMode.Force);
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "platform")
{
isGrounded = true;
hasDouble = true;
movementSpeed = 15;
}
}
/*void FixedUpdate()
{
isGrounded = Physics.Raycast(transform.position, - Vector3.up, 1.0f);
if (isGrounded)
{
Jump(); //Automatic jumping
}
}*/
}