Question by
simplicitydown · Jan 10, 2016 at 11:02 PM ·
c#aiaddforce
Enemy watches player and shoots but bullets will not project
So I am pretty happy with how well this script works, despite it’s one issue. This is a 2D topdown game and this script makes an object continuously rotate on a spot to always be facing the player, and continously spawns bullets in that direction. The only problem is that, of course, I am stuck on the last detail of making the bullets project when they spawn. This could be a pretty useful little AI for anyone to use if that last detail was mended…I commented out a few of my other attempts to solve that problem from searching around that didn’t work to show some of what has already been attempted. Thanks so much in advance!
using UnityEngine;
using System.Collections;
public class sightPlayer : MonoBehaviour {
public GameObject Player;
public GameObject watcherShot;
public Transform watcherShotPoint;
public Rigidbody2D wShotRig;
public float wShotforceRate = 500 ;
public void Start()
{
wShotRig = GetComponent<Rigidbody2D>() ;
}
void Update () {
Vector3 dir = Player.transform.position – transform.position;
float angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
StartCoroutine(“WShootEvent”);
}
public IEnumerator WShootEvent()
{
yield return new WaitForSeconds(2f);
GameObject wShot = (GameObject)Instantiate(watcherShot, watcherShotPoint.transform.position, Quaternion.identity);
// wShotRig.AddForce(wShotforceRate*Vector2.MoveTowards,Vector2.*Time.deltaTime) ;
wShotRig.AddForce(transform.TransformDirection * 50000) ;
// wShotRig.AddForce(transform.TransformDirection * 50000) ;
//wShotRig.rigidbody.AddForce(swipeDirection * 5);
yield return new WaitForSeconds(2f);
}
}
Comment