Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Created November 10, 2024 15:08
Show Gist options
  • Select an option

  • Save unitycoder/f1f85246b1977175d06e79c81002bd4a to your computer and use it in GitHub Desktop.

Select an option

Save unitycoder/f1f85246b1977175d06e79c81002bd4a to your computer and use it in GitHub Desktop.
Projectile motion Unity 3D to always make a Hit to moving object
// https://web.archive.org/web/20150428033712/http://lexiconhub.tk/index/game-programming/projectile-motion-unity-3d/
using UnityEngine;
using System.Collections;
public class enemy : MonoBehaviour
{
public float MinSpeed;
public float MaxSpeed;
public float currentSpeed;
private float x, y, z;
void Start () {
setposandspeed ();;
}
void Update () {
float amttomove = currentSpeed * Time.deltaTime;
transform.Translate (Vector3.left * amttomove);
if (transform.position.x < 0f)
setposandspeed();
}
void setposandspeed()
{
x = 11.5f;
z = 0.0f;
currentSpeed = Random.Range (MinSpeed, MaxSpeed);
y = Random.Range (0f, 2.5f);
transform.position = new Vector3(x, y, z);
}
}
using UnityEngine;
using System.Collections;
public class player : MonoBehaviour {
public float playerspeed;
public static int score = 0;
public GameObject bulletprefab;
public Transform enemy;
public projectile _pro;
public GameObject g;
void Start()
{
g = GameObject.Find ("bulletprefab");
}
// Update is called once per frame
void Update ()
{
float amttomove = Input.GetAxisRaw ("Horizontal") * playerspeed * Time.deltaTime;
transform.Translate (Vector3.right * amttomove);
if (transform.position.x <= -7.0f)
{
transform.position = new Vector3 (-7.0f, transform.position.y, transform.position.z);
}
else if (transform.position.x >= 0f)
{
transform.position = new Vector3 (0f, transform.position.y, transform.position.z);
}
if (Input.GetKeyDown ("space"))
{
Vector3 position = new Vector3 (transform.position.x, transform.position.y + collider.bounds.size.y / 2);
GameObject go= GameObject.Find("enemy");
Transform playerTransform = go.transform;
Vector3 posi = new Vector3(playerTransform.position.x, playerTransform.position.y, playerTransform.position.z);
_pro = g.GetComponent<projectile>();
_pro.Target.transform.position = new Vector3(posi.x, posi.y, posi.z);
_pro.SetTarget(_pro.Target.transform.position);
var def = Instantiate (bulletprefab, position, Quaternion.identity);
Destroy(def, 1);
}
}
void OnGUI()
{
BuildUI ();
}
void BuildUI()
{
GUI.Label (new Rect(10, 10, 120, 20), "Score: " + player.score.ToString());
}
}
using UnityEngine;
using System.Collections;
public class projectile : MonoBehaviour
{
public Transform Target;
public GameObject bulletprefab;
public float firingAngle = 45.0f;
public float gravity = 9.8f;
public Transform Projectile;
private Transform myTransform;
public static Vector3 demo;
public void SetTarget(Vector3 updatedposition)
{
demo = updatedposition;
}
void Awake()
{
myTransform = transform;
}
void Start()
{
myTransform.LookAt(demo);
StartCoroutine (ProjectileMotion ());
}
IEnumerator ProjectileMotion()
{
yield return new WaitForSeconds(0.0001f);
Projectile.position = myTransform.position + new Vector3(0, 0.0f, 0);
// Calculating distance to target
float target_Distance = Vector3.Distance (Projectile.position, demo);
float projectile_Velocity = target_Distance / (Mathf.Sin(firingAngle * Mathf.Deg2Rad) / gravity);
// Extract the X Y componenent of the velocity
float Vx = Mathf.Sqrt(projectile_Velocity) * Mathf.Cos(target_Distance* Mathf.Deg2Rad);
float Vy = Mathf.Sqrt(projectile_Velocity) * Mathf.Sin( target_Distance * Mathf.Deg2Rad);
float flightDuration = target_Distance / Vx;
// projectile rotated at target
Projectile.rotation = Quaternion.LookRotation(demo - Projectile.position);
float elapse_time = 0;
while (elapse_time < flightDuration) //looping and incrementing elapsed time
{
Projectile.Translate(0, (Vy - (gravity * elapse_time)) * Time.deltaTime, Vx * Time.deltaTime);
elapse_time += Time.deltaTime;
yield return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment