Quantcast
Channel: Questions in topic: "parenting"
Viewing all articles
Browse latest Browse all 302

Dragging Enemies C#

$
0
0
I'm having some trouble coding a script that drags the enemies to different positions (it would follow the player). using UnityEngine; using System.Collections; [RequireComponent(typeof(SphereCollider))] public class PlayerDrag : MonoBehaviour { public float grabDistance = 1; private PlayerController playerController; private SphereCollider col; private Transform enemy; private EnemyAI enemyAI; private EnemySight enemySight; private EnemyFacePlayer enemyFacePlayer; void Awake () { playerController = GameObject.FindWithTag("Player").GetComponent (); col = GetComponent (); enemy = GameObject.FindWithTag ("Enemy").transform; enemyAI = GameObject.FindWithTag ("Enemy").GetComponent(); enemySight = GameObject.FindWithTag ("Enemy").GetComponent(); enemyFacePlayer = GameObject.FindWithTag ("Enemy").GetComponent(); } void OnTriggerStay (Collider other) { if (other.gameObject == enemy) { if (playerController.sneaking == true) { RaycastHit hit; if(Physics.Raycast(transform.position, Vector3.forward, out hit, col.radius)) { if (hit.collider.gameObject == enemy) { if (Input.GetButtonDown ("Drag")) { hit.transform.parent = transform; enemyAI.enabled = false; enemySight.enabled = false; enemyFacePlayer.enabled = false; }else{ hit.transform.parent = null; } } } } } } } Basically, I'm trying to parent the enemy to the player, so he moves with the player when a certain button is pressed. It could be a simple mistake as I am somewhat new to coding in Unity. But if anyone sees a problem or has a better way of doing this, please answer with what I should do.

Viewing all articles
Browse latest Browse all 302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>