-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTankControl.cs
40 lines (36 loc) · 1.27 KB
/
TankControl.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
37
38
39
40
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TankControl : MonoBehaviour
{
public float forceScale = 1.0f;
public GameObject Drone;
private Rigidbody rig;
void Start()
{
rig = gameObject.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float spd = rig.velocity.magnitude;
if (spd <= 30f)
{
rig.AddForce(transform.forward * forceScale, ForceMode.Force);
}
float dis = Vector3.Distance(transform.position, Drone.transform.position);
if (dis > 500f)
{
Vector3 currentDirection = transform.forward;
var targetPosition = new Vector3(Drone.transform.position.x,transform.position.y,Drone.transform.position.z);
Vector3 targetDirection = (targetPosition - transform.position).normalized;
Vector3 newDirection = Vector3.Lerp(currentDirection, targetDirection, Time.deltaTime * 2);
transform.rotation = Quaternion.LookRotation(newDirection);
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawLine(transform.position, transform.forward * forceScale);
}
}