# Transform.forward problem (789712)

**URL:** https://discussions.unity.com/t/transform-forward-problem-789712/789712
**Category:** Scripting
**Tags:** Scripting
**Created:** [May 8, 2020, 3:11am UTC](https://discussions.unity.com/t/transform-forward-problem-789712/789712 "2020-05-08T03:11:50Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![chengwang2077](https://avatars.discourse-cdn.com/v4/letter/c/f9ae1b/32.png) [@chengwang2077](https://discussions.unity.com/u/chengwang2077)
#### Post date: [May 8, 2020, 3:11am UTC](https://discussions.unity.com/t/transform-forward-problem-789712/789712/1 "2020-05-08T03:11:50Z")

</div>

My game object’s rotation equals (0,0,180),and My code is as follows

void start(){  
var forward = transform.forward;  
transform.forward = forward;  
}

after code execution  
game object’s rotation equals (0,0,0)

why?

And how to set tranform forward without reset Z coodinate?

---

<div class="post-metadata">

### Author: ![PraetorBlue](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/praetorblue/32/613450_2.png) [@PraetorBlue](https://discussions.unity.com/u/PraetorBlue)
#### Post date: [May 8, 2020, 3:18am UTC](https://discussions.unity.com/t/transform-forward-problem-789712/789712/2 "2020-05-08T03:18:59Z")

</div>

Try putting some logging statements in there… I think your problem might be in different code:

```csharp
void start(){
  var forward = transform.forward;
  Debug.Log("Forward before: ${transform.forward}");
  transform.forward = forward;
  Debug.Log("Forward after: ${transform.forward}");
}

```

---

<div class="post-metadata">

### Author: ![chengwang2077](https://avatars.discourse-cdn.com/v4/letter/c/f9ae1b/32.png) [@chengwang2077](https://discussions.unity.com/u/chengwang2077)
#### Post date: [May 8, 2020, 3:36am UTC](https://discussions.unity.com/t/transform-forward-problem-789712/789712/3 "2020-05-08T03:36:47Z")

</div>

```csharp
using UnityEngine;

public class test : MonoBehaviour
{
    void Start()
    {
        Debug.Log("before" + transform.eulerAngles.z);
        transform.forward = transform.forward;
        Debug.Log("after" + transform.eulerAngles.z);
    }
}

```

Create a GameObject and set rotation z to 180 , and add this component to the gameobject,  
it will log  
before180  
after0

---

<div class="post-metadata">

### Author: ![PraetorBlue](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/praetorblue/32/613450_2.png) [@PraetorBlue](https://discussions.unity.com/u/PraetorBlue)
#### Post date: [May 8, 2020, 3:39am UTC](https://discussions.unity.com/t/transform-forward-problem-789712/789712/4 "2020-05-08T03:39:48Z")

</div>

Ohh you’re totally right. Ok I think I know what’s happening here. Instead of doing transform.forward = transform.forward try this:

```csharp
transform.LookAt(transform.forward, transform.up);

```

basically when you set transform.forward to a value it calls Transform.LookAt with only one parameter. In that mode the “up” direction defaults to Vector3.up.

The reason it does this is because just specifying a place to look is not enough info to fully figure out the rotation. You also need to tell Unity which direction “up” should be.

---

<div class="post-metadata">

### Author: ![chengwang2077](https://avatars.discourse-cdn.com/v4/letter/c/f9ae1b/32.png) [@chengwang2077](https://discussions.unity.com/u/chengwang2077)
#### Post date: [May 8, 2020, 3:47am UTC](https://discussions.unity.com/t/transform-forward-problem-789712/789712/5 "2020-05-08T03:47:58Z")

</div>

Completely solved my problem, thank you
