-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdinoscript
More file actions
65 lines (58 loc) · 1.66 KB
/
dinoscript
File metadata and controls
65 lines (58 loc) · 1.66 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character : MonoBehaviour //monobehaviour required for code. character should be connector
{
// components gaeee
public Rigidbody2D rb;
// player movements
float walkSpeed = 4.0f;
float SpeedLimiter = 0.7f;
float inputVertical;
float inputHorizontal;
bool facingright = true;
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();
}
void Update()
{
inputHorizontal = Input.GetAxisRaw("Horizontal");
inputVertical = Input.GetAxisRaw("Vertical");
// CharacterController.Move(move * Time.DeltaTime * Speed);
}
void FixedUpdate()
{
if (inputHorizontal != 0 || inputVertical != 0)
{
if (inputHorizontal != 0 && inputVertical != 0)
{
rb.velocity = new Vector2(inputHorizontal *= SpeedLimiter, inputVertical *= SpeedLimiter);
}
rb.velocity = new Vector2(inputHorizontal * walkSpeed, inputVertical * walkSpeed);
}
else
{
rb.velocity = new Vector2(0f, 0f);
}
if (inputHorizontal !=0)
{
rb.AddForce(new Vector2(inputHorizontal * walkSpeed, 0f));
}
if (inputHorizontal > 0 && !facingright)
{
Flip();
}
if (inputHorizontal < 0 && facingright)
{
Flip();
}
}
void Flip()
{
Vector3 currentScale = gameObject.transform.localScale;
currentScale.x *= -1;
gameObject.transform.localScale = currentScale;
facingright = !facingright;
}
}