-
Notifications
You must be signed in to change notification settings - Fork 0
/
Movimiento.cs
36 lines (25 loc) · 936 Bytes
/
Movimiento.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Movimiento Coche Principal clase Movimiento.
public class Movimiento : MonoBehaviour {
Rigidbody2D rb;
[SerializeField]
float accelerationPower = 5f;
[SerializeField]
float steeringPower = 5f;
float steeringAmount, speed, direction;
// Inicializar
void Start () {
rb = GetComponent<Rigidbody2D> ();
}
// Update, una vez por frame crea el movimiento
void FixedUpdate () {
steeringAmount = - Input.GetAxis ("Horizontal");
speed = Input.GetAxis ("Vertical") * accelerationPower;
direction = Mathf.Sign(Vector2.Dot (rb.velocity, rb.GetRelativeVector(Vector2.up)));
rb.rotation += steeringAmount * steeringPower * rb.velocity.magnitude * direction;
rb.AddRelativeForce (Vector2.up * speed);
rb.AddRelativeForce ( - Vector2.right * rb.velocity.magnitude * steeringAmount / 2);
}
}