transform.localPosition += transform.forward moves object toward (0, 0, 0)
Basically I want this gameObejct to move towards this plant.
public float speed;
public GameObject plant;
void Update () {
Debug.Log (transform.forward);
if (plant != null) {
transform.LookAt (plant.transform);
} else {
transform.LookAt (transform.forward);
}
transform.localPosition += transform.forward * Time.deltaTime * speed;
}
void OnCollisionEnter(Collision col){
if (col.gameObject.tag != "Wall") {
transform.rotation = Quaternion.Euler (0, 0, Random.Range (0.1f, 359.9f));
if (plant != null) {
Destroy (plant);
plant = null;
}
} else {
transform.rotation = Quaternion.Euler (0, 0, 90);
}
}
It starts out by going to the plant, and once it hits it, it destroys the plant and it faces (0, 0, 0) and wont face or move anywhere else. If anyone has any idea of what happening and how to fix it please let me know because this is turning my hairs grey with frustration. Thanks!
I think we need more context. What is setting the plant field?
Answer by MaxGuernseyIII · Sep 01, 2017 at 12:22 AM
If I had to guess, from what you've offered, it would be that you are setting plant to null after you collide with it and (I suspect) nothing then sets it to point to another plant after the collision and destruction occurs. As I stated in my comment, though, to give you a solid answer, we would need more context.
Answer by boogiebob9 · Sep 01, 2017 at 01:24 AM
Unity's website is acting weird, but in short I made the object just go forward using transform.forward and it still made it go to 0, 0, 0.
Are you sure it's actually going toward 0, 0, 0?
Could it be treating it's new target as an address in world space and thus going to the origin + direction (which would also take it through the origin, if that's where the object started)?
If you just want it to keep going in the same direction, why are you calling LookAt(transform.forward) at all?
Your answer
![](https://koobas.hobune.stream/wayback/20220612133954im_/https://answers.unity.com/themes/thub/images/avi.jpg)