- Home /
Question by
Cryno1000 · Jun 15, 2020 at 09:26 PM ·
scripting problemprojectilemovetowards
Can someone tell me how to make a projectile shoot at a Transform without moving to the Transform's new position?
So I have this script for enemies to shoot projectiles at the player and I want one to track the player as they move around and the others to just shoot to where the player was before they moved. Could someone tell me how I could make it so that if the plasma type is not 3, it moves to where the player was when the projectile was created?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Plasma : MonoBehaviour
{
public int plasmaType;
/* Plasma Types
* 1 = Red - Explosive, Slow
* 2 = Blue - Doesn't Track, Fast
* 3 = Green - Tracks, Slow
*/
public Transform target;
private Transform stationaryTarget; //where the player was when the projectile was created (SHOULD NOT UPDATE)
public float speed;
void Start()
{
target = GameObject.FindGameObjectWithTag("Player").transform;
stationaryTarget = target; //how can I make it so that this stays constant and doesn't update?
}
void Update()
{
if (plasmaType != 3) //Doesn't track player
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, stationaryTarget.position, step);
}
if (plasmaType == 3) //Tracks player
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}
public void OnCollisionEnter(Collision collision)
{
Destroy(gameObject); //Destroys the plasma if it collides with anything
}
}
Thanks in advance
Comment
Best Answer
Answer by kiramikk · Jun 15, 2020 at 09:41 PM
change the type of stationaryTarget to vector3 instead, and in Start() stationaryTarget = target.position and you move towards stationaryTarget.
Should give you exactly what you want i think.
Your answer
![](https://koobas.hobune.stream/wayback/20220613001030im_/https://answers.unity.com/themes/thub/images/avi.jpg)