Here is my situation. I’m trying to make a turret that shoots shells (I already have it set as a child object to a tank). I’m trying to get it to where the shell will be shot in a direction that is based on the rotation of the turret. However, I have tried several things and nothing worked. Here is my code for the bullet
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Bullet : MonoBehaviour
{
public int damage =1;// Use this for initialization
public bool isEnemyShot = false;
public Vector2 speed= new Vector2(10,0);
//public int aimAngle = TankFire.instance.aimAngle;
public Vector2 direction = new Vector2 (1, 0);
private Vector2 movement;
void Start ()
{
DestroyObject (gameObject, 20);//20sec, need to destory to avoid leak
//movement = new Vector2(speed.x, speed.y);
movement = new Vector2(2045, 2045);
rigidbody2D.AddForce (movement);
}
void Update()
{
//rigidbody2D.velocity = transform.forward;
}
void FixedUpdate()
{
//rigidbody2D.velocity = movement;
//apply movement to the rigidbody
}
}
And here is my code for the object shooting it
if(tick >= fireRate)
{
//Instantiate (tankBullet, this.transform.position, Quaternion.Euler (this.transform.rotation.eulerAngles.z, this.transform.rotation.eulerAngles.y,this.transform.rotation.eulerAngles.z));
Instantiate (tankBullet, this.transform.position,this.transform.rotation);
tick = 0;
}
I also need the shell to drop due to gravity using the Unity physics engine.
Thanks to whoever is able to help.