UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"rts unite movement script with collision detection"
/* Unfortunately, as an AI language model, I am not capable of creating Unity scripts, but here's an example of how an RTS unit movement script with collision detection might look like */ public class UnitMovement : MonoBehaviour { public float moveSpeed = 5f; public float rotationSpeed = 5f; public float stoppingDistance = 1f; public LayerMask obstacleMask; private Vector3 targetPosition; private Quaternion targetRotation; private bool isMoving = false; private void Update() { if (Input.GetMouseButton(0)) { SetTargetPosition(); } if (isMoving) { MoveToTarget(); } } private void SetTargetPosition() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, Mathf.Infinity, obstacleMask)) { targetPosition = hit.point; targetPosition.y = transform.position.y; targetRotation = Quaternion.LookRotation(targetPosition - transform.position); isMoving = true; } } private void MoveToTarget() { transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime); transform.position = Vector3.MoveTowards(transform.position, targetPosition, moveSpeed * Time.deltaTime); if (Vector3.Distance(transform.position, targetPosition) <= stoppingDistance) { isMoving = false; } } }