- Home /
gameObject.transform.forward does not return expected value
I've imported a bullet 3D model in my game, and I had to rotate it so the prefab bullet is facing forward. I'm using a script that moves the gameObject forward, and it works on my sphere bullet, but not on this imported bullet.
In the picture I'm expecting gameObject.transform.forward == (1, 0, 0)
but it actually equals (0, -1, 0)
This is the MoveBullet
script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveBullet : MonoBehaviour {
private GameObject bullet;
public float velocityMultiplier = 0.4f;
// Use this for initialization
void Start () {
bullet = this.gameObject;
}
// Update is called once per frame
void Update () {
bullet.transform.position += bullet.transform.forward * velocityMultiplier;
print ("bullet forward vector: " + bullet.transform.forward);
}
}
Solution found, I just use the transform.up vector of the bullet ins$$anonymous$$d of the forward vector. An alternative solution may have been to use Blender to rotate the model then import that .fbx
file into Unity so that it's already facing the correct direction with rotation of (0, 0, 0)
in Unity.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class $$anonymous$$oveBullet : $$anonymous$$onoBehaviour {
private GameObject bullet;
private Vector3 unitVectorDirection;
public float velocity$$anonymous$$ultiplier = 0.4f;
public bool upIsForward = false;
// Use this for initialization
void Start () {
bullet = this.gameObject;
unitVectorDirection = upIsForward ? bullet.transform.up : bullet.transform.forward;
}
// Update is called once per frame
void Update () {
bullet.transform.position += unitVectorDirection * velocity$$anonymous$$ultiplier;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612103212im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Multiple Cars not working 1 Answer
Rotate object around another object C# 2 Answers
HoloLens - Smooth Rotation doesn't work 0 Answers