- Home /
Homing projectile
alright so new to programming sure its something simple so I have my enemy like character looking at the player and shooting but I want to bullet to homing onto player also so trying to use the method of make bullet face player and move towards player here is what I ended up with any help would be great :)
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TrackingProjectile : MonoBehaviour { public float speed = 3.0f; public GameObject m_target = null; Vector3 m_lastKnownPosition = Vector3.zero; Quaternion m_lookAtRotation; public float MoveSpeed = 3.0f;
// Update is called once per frame
void Update () {
if (m_target) {
if (m_lastKnownPosition != m_target.transform.position) {
m_lastKnownPosition = m_target.transform.position;
m_lookAtRotation = Quaternion.LookRotation (m_lastKnownPosition - transform.position);
}
if (transform.rotation != m_lookAtRotation) {
transform.rotation = Quaternion.RotateTowards (transform.rotation, m_lookAtRotation, speed * Time.deltaTime);
}
}
}
bool SetTarget(GameObject target){
if (target){
return false;
}
m_target = target;
return true;
{if (m_target) {
transform.Translate (Vector3.forward * MoveSpeed * Time.deltaTime);
}
}
} }
In your SetTarget
method you return false if the passed parameter target
is not null. Did you mean to check if passed target
IS null or did you mean to check if current value m_target
isn't null, ins$$anonymous$$d of what you have written?
Also, the code after return
won't be executed, since the function stops execution when it returns.
so, what is it doing and how does that differ from what you'd like it to do?
Your answer
Follow this Question
Related Questions
homing bullets 1 Answer
Character always follow straight help!!! 0 Answers
Homing Missiles that target where you were 0 Answers
Bullet going in direction of moving object 1 Answer
Having A Missile Follow An Enemy (Heat Seeker/Homing) 2 Answers