Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

C# - 2d --I have two questions... both seem like they should be incredibly simple - script hive mind + MoveTo not follow

$
0
0
I'm trying to solve two problems I can't figure out. First - with the following code I create a delay - so the enemy waits a tick after spawning before shooting. Problem 1 -- This delay works - but - every spawned instance w/ the script -- has the exact same delay -- and enemies spawned later don't have a delay. == how do I even...? I thought Start() was for that instance of the script? What should I use? void Start () { nextAttack = Time.deltaTime * (Time.time + attackSpeed + Random.Range(-0.5f,0.5f)); } Second, another problem I've been having is -- moving in the direction of a saved location -- rather than following the thing that it should have saved the location of. I figured the problem was in update -- so I've tried moving it to start -- the following is my latest attempt -- this i've no fcking clue what this is doing -- it actually moves -- but in a random direction -- w/ some bullets travelling at a different speed than others...Idk why -- The commented lines don't actually work -- other than Vector2.Left -- quite a few didn't actually move at all -- so needed to check it wasn't somewhere else causing the problem public float projectileSpeed; public bool hasPattern; public bool aimsAtPlayer; private Rigidbody2D myRigidBody; public GameObject myTarget; public Transform targetLocation; // Use this for initialization void Start () { myRigidBody = GetComponent(); targetLocation = myTarget.transform; if (!aimsAtPlayer){ myRigidBody.velocity = Vector2.left * projectileSpeed; } if (aimsAtPlayer) { myRigidBody.velocity = Vector2.MoveTowards(-myRigidBody.gameObject.transform.position, targetLocation.position, projectileSpeed); //myRigidBody.gameObject.transform.LookAt(myTarget.transform); //myRigidBody.transform.position = Vector2.MoveTowards(myRigidBody.transform.position,myTarget.transform.position,projectileSpeed * Time.deltaTime); //myRigidBody.velocity = Vector2.left * projectileSpeed * Time.deltaTime; //myRigidBody.transform.position = Vector2.Lerp(myRigidBody.transform.position, myTarget.transform.position, projectileSpeed * Time.deltaTime); } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles