mirror of
https://github.com/frizbee19/FebGameJam.git
synced 2026-09-11 08:17:01 +00:00
did a minor tweak
This commit is contained in:
1 parent
8f2a693284
commit
11a2af2f5f
302 files changed
+1022
-167
No files matched your search
@@ -0,0 +1,40 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class CarScript : MonoBehaviour {
|
||||
public float speed = 3f;
|
||||
|
||||
private Rigidbody2D rb;
|
||||
public Vector2 startPositionRelativeToTransform;
|
||||
public Vector2 endPositionRelativeToTransform;
|
||||
|
||||
private Vector2 absStartPosition, absEndPosition;
|
||||
|
||||
void Start() {
|
||||
rb = this.GetComponent<Rigidbody2D>();
|
||||
absStartPosition = startPositionRelativeToTransform + rb.position;
|
||||
absEndPosition = endPositionRelativeToTransform + rb.position;
|
||||
}
|
||||
|
||||
void FixedUpdate() {
|
||||
if (Movement.pause) return;
|
||||
// Calculate distance to end
|
||||
Vector2 distanceToFinish = absEndPosition - rb.position;
|
||||
|
||||
if (distanceToFinish.magnitude < 1) {
|
||||
// If we're close enough to the end, reverse direction
|
||||
Vector2 temp = absStartPosition;
|
||||
absStartPosition = absEndPosition;
|
||||
absEndPosition = temp;
|
||||
transform.localScale = new Vector3(-transform.localScale.x, 1, 1);
|
||||
}
|
||||
|
||||
Vector2 direction = absEndPosition - rb.position;
|
||||
|
||||
moveMe(direction.normalized);
|
||||
}
|
||||
void moveMe(Vector2 direction) {
|
||||
rb.MovePosition(rb.position + direction * speed * Time.fixedDeltaTime);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user