I thought this would be simple but, after hours of research and troubleshooting, I just can’t figure out what I’m doing wrong.
I’m trying to make a projectile move toward the position of the player when the projectile is instantiated (the player can move out of the way and the projectile will continue to move in the same direction).
MoveTowards obviously didn’t work and none of the other answers I could find did the trick. The projectile just falls straight down.
Here’s my code, along with all the stuff I tried but didn’t work:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class throwntowardplayer : MonoBehaviour
{
static GameObject S = GameObject.Find("sack");
Vector3 sposition;
Vector3 tposition;
Vector3 direction;
// Start is called before the first frame update
void Start()
{
sposition = S.transform.position;
tposition = transform.position;
direction = (sposition - tposition);
direction.Normalize();
}
// Update is called once per frame
void Update()
{
transform.Translate(direction * 5 * Time.deltaTime);
}
}
//private GameObject Sack = GameObject.Find("sack");
//public Vector3 sackLocation;
//public Vector3 Direction;
//private float speed = 1f;
//private Transform sackTransform;
//private Transform selfTransform;
//sackTransform = Sack.GetComponent<Transform>();
//sackLocation = sackTransform.transform.position;
//selfTransform = GetComponent<Transform>();
//Direction = (sackLocation - selfTransform.transform.position).normalized;
//selfTransform.transform.position += Direction* speed * Time.deltaTime;
//transform.position = Vector3.MoveTowards(transform.position, Sacklocation, .2f);
//transform.Translate(Direction * Time.deltaTime, Space.World);
//transform.position -= transform.forward * Time.deltaTime;