- Home /
Question by
soemyatmin · Jul 30, 2016 at 05:47 AM ·
raycastvector3movetowardsthrowingtps
Shotting Or thrwoing Object wtih Vector3.MoveTowards
Im make TPS game. I want to do is throwing an object to wards the enemy or some target slowly
so im using Vector3.MoveTowards
Problem is The TARGET is set as like this
then if the Aim Camera is moving Left or Right the Object that already thrown is moving in the air Left or Right
What is the best way to fix this. is there any other way to to make throwing item towards the target?
Here is my code
The gun script in update()
if (shot) {
shot = false;
ray = AimmingCamera.ViewportPointToRay (new Vector3 (0.5F, 0.5F, 0));
if (Physics.Raycast (ray, out hit) && hit.transform.tag =="Enemy") {
Shotting.TARGET = hit.transform;
}else{
Shotting.TARGET = nextTarget;
}
Instantiate (fruit[Random.Range(0,fruit.Length)], transform.position, Quaternion.identity);
}
And Instantiated Pref has
public static Transform TARGET;
public float speed = 10;
void Start () {
Destroy (gameObject, 3.0f);
}
void Update() {
if (TARGET) {
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards (transform.position, TARGET.position, step);
}
}
target.png
(83.6 kB)
Comment
Best Answer
Answer by soemyatmin · Jul 30, 2016 at 06:06 AM
I got now. because of static variable.
public static Transform TARGET;
Vector3 localTarget = new Vector3();
public float speed = 10;
void Start () {
Destroy (gameObject, 3.0f);
localTarget = TARGET.transform.position;
}
void Update() {
if (TARGET) {
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards (transform.position, localTarget, step);
}
}