My final goal was to have point A and point B and use movetowards to smoothly transverse these two points
Solution - A - - - - B
I used the Vector3 Movetowards method to achieve this but I am not getting the desire effect.
Instantiate (laser, estart.transform.position, this.transform.rotation);
laser.transform.position = Vector3.MoveTowards(estart.transform.position, playerpos, Time.deltaTime * 0.3f);
I was sure that:
estart,transform would = point A
playerpos is the end result of point B
and my time.deltaTime * speed would be the time in order to transverse these points.
However, rather than get that effect I get nothing in which the laser simply stays on the estart position and moves using the laserspeed without any real destination so simply the same as had i used just the rigidbody2D.velocity
in case this is important ill add several points:
I have not set the velocity of the object in the object’s script in this example as this is being done when instantiated and does not move and when i do this it simply goes in the direction of velocity not following the A-B points.
Next im doing this off the ground so not using gravity as i want the laser to instantiate and then fly towards the playerpos
I have tried many attempts at getting this working from using raycasts to animation to other vector3 methods however from what i see in unity documentation this seems to be the method i am needing so any help would be appreciated as this is the final part to the boss. Thanks
EDIT:
here is my updated code
using UnityEngine;
using System.Collections;
public class FindPlayer : MonoBehaviour
{
public bool clonecreate = true;
public Vector3 playerpos;
public bool spotted = false;
public GameObject laser;
public GameObject estart;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (clonecreate == false) {
Debug.Log ("hello there im in the update");
float step = Time.deltaTime * 2.0f;
if (laser != null) {
if (laser.transform.position != playerpos) {
laser.transform.position = Vector3.MoveTowards (estart.transform.position, playerpos, step);
}
}
clonecreate = true;
}
}
IEnumerator OnTriggerStay2D (Collider2D target)
{
if (target.gameObject.tag == "Player") {
if (spotted == false) {
Vector3 targetcaught = target.transform.position;
spotted = true;
playerpos = targetcaught;
yield return StartCoroutine (Delay (2.0f));
}
}
}
IEnumerator Delay (float wait)
{
if (spotted) {
//Debug.Log ("Delay " + Time.time);
yield return new WaitForSeconds (wait);
//Debug.Log ("Delay finished " + Time.time);
Instantiate (laser, estart.transform.position, this.transform.rotation);
Debug.Log ("this is being instantiated");
clonecreate = false;
if(clonecreate == true)
spotted = false;
}
}
}
im really hoping this is a daft error on my part as im stumped as to why this is happening