Hello everyone!
So, my scripting skills are still at a baby level and I can’t figure out how can I take Vector3 data from different object and store it in variable at start of the code, so I can refer to it in void Update.
To make myself more clear, I will show the code that works:
void Update()
{
float step = speed * Time.deltaTime;
projectile.transform.position = Vector3.MoveTowards(projectile.transform.position, Target.transform.position, step);
}
}
And it does exactly what I want, the projectile moves at the position provided. But, when I replace “projectile.transform.position” and “Target.transform.position” with variables value1 and value2, it just doesn’t work anymore. How is that if that’s two same pieces of code, but written in different way?
I tried to initialize it in void Start:
void Start ()
{
Target = GameObject.Find("TARGET_AIM");
Destroy(gameObject, 10.0f);
Vector3 value1 = (projectile.transform.position);
Vector3 value2 = (Target.transform.position);
}
It doesn’t give me any errors, warnings or anything, I can run the game, but projectiles just move into one fixed point in the world without any reason.
I tried with “var value1” and “var value2” but it also doesn’t work.
What I actually want to do? So, I want the projectiles to move at the position of the “Target”, but not actually follow it, that’s why I’m trying to save the Vector3 position into variable, so the point will be fixed and the projectile will not change the direction when the “Target” moves.
At last, there’s the whole script, from start to end:
public class FriendlyProjectileController : MonoBehaviour {
public Rigidbody rb;
public float speed;
public GameObject projectile;
public Transform projectile_tr;
public GameObject Particle;
public Transform position;
private GameObject Target;
private Vector3 TargetPos;
private Vector3 value1;
private Vector3 value2;
void Start ()
{
Target = GameObject.Find("TARGET_AIM");
Destroy(gameObject, 10.0f);
Vector3 value1 = (projectile.transform.position);
Vector3 value2 = (Target.transform.position);
}
private void OnCollisionStay(Collision col)
{
Destroy(gameObject);
Instantiate(Particle, position.position, position.rotation);
}
void Update()
{
float step = speed * Time.deltaTime;
projectile.transform.position = Vector3.MoveTowards(value1, value2, step);
}
}
I spent some time searching and couldn’t figure it out myself. If I’m not clear enough, tell me and I’ll try to clarify. I hope it’s nothing dumb that I just can’t see. Can you help me guys?