- Home /
same script work on capsule but transform problem with turret model
hi guys i have script which i have attached with it with capsule. it works fine the capsule loot at me and fire at me when at specific position but the problem is that when i attached the same script to the imported model of turret gun it rotates upside down here is the image help me guys i dont know what i am missing :
before running
during running this is the problem I I I V
as you can see the turret gun go upside down and stand vertically . it can look at me and rotate where i go and also shoot me but why is it upside down
. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class aiEasy : MonoBehaviour { public GameObject bulletPrefab; public Transform bulletSpawn; private AudioSource gunAudio; public float fireRate = .25f; private float nextFire; private float lastShot=0.0f; public float fireRate2=0.5f;
public int gunDamage = 1;
private WaitForSeconds shotDuration = new WaitForSeconds(0.07f);
public float fpsTargetDistance;//the distnce of our fps controller
public float enemyLookDistance;//how much enemy will look when we have been seen
public float attackDistance;
public float enemyMovementSpeed;
public float damping;
public Transform fpsTarget;//fps our
Rigidbody theRigidbody;
Renderer myRender;
public float delta=5.5f;
public float speedy =2.0f;
private Vector3 startPos;
// Use this for initialization
void Start () {
myRender = GetComponent<Renderer>();
theRigidbody = GetComponent<Rigidbody>();
gunAudio = GetComponent<AudioSource>();
startPos = transform.position;
}
// Update is called once per frame
void FixedUpdate () {
/* Vector3 v = startPos;
v.x += delta * Mathf.Sin (Time.time * speedy);
transform.position = v;
*/
fpsTargetDistance = Vector3.Distance (fpsTarget.position,transform.position);
if (fpsTargetDistance<enemyLookDistance)
{
myRender.material.color = Color.yellow;
lookAtPlayer ();
//print ("look at player please");
}
if (fpsTargetDistance<attackDistance)
{
myRender.material.color = Color.red;
// transform.Translate=0;
//move after you
// attackPlease();
// print("Attack!!");
Fire();
}
else
{
myRender.material.color = Color.blue;
}
}
void lookAtPlayer()
{
Quaternion rotation =Quaternion.LookRotation(fpsTarget.position-transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation,rotation,Time.deltaTime+damping);
}
void attackPlease()
{
theRigidbody.AddForce(transform.forward*enemyMovementSpeed);//remember to remove this because player follow you
}
void Fire()
{
if (Time.time > fireRate2+lastShot)
{
Debug.Log("this is space pressed");
// Create the Bullet from the Bullet Prefab
var bullet = (GameObject)Instantiate(bulletPrefab,transform.position,transform.rotation) ;
string xx = bulletSpawn.position.ToString();
Debug.Log("thisssss is soawn buller postion"+ xx);
// Add velocity to the bullet
bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 50;
// PlayerController newBullet = (PlayerController)(Instantiate(bulletPrefab, bulletSpawn, Quaternion.identity) as GameObject);
//PlayerController.direction = bulletSpawn.forward;
lastShot = Time.time;
// Destroy the bullet after 2 seconds
Destroy(bullet, 2.0f);
}
}
private IEnumerator ShottEffect(){
gunAudio.Play ();
//laserLine.enabled = true;
yield return shotDuration;
// laserLine.enabled = false;
}
}
Have you bothered to check wither or not your 3D models transform.up is actually UP? Often times models get rotated or moved in a 3D editor and when imported, their facing direction is not Unitys normal forward. To solve this, you place the object inside of an empty game object, and make it face forward equivalent to the parent object.
Answer by Piyush_Pandey · May 10, 2017 at 10:21 AM
This is happening because the orientation of your gun is different.Y and Z are flipped. In your code you used a correct method but a different overload. U can see from your image that the Z and Y axes are changing positions with Z as forward-> towards the camer/player{correct} but the upwards which is the Y axis is changing{wrong}.You have to set the upwards direction correctly. In your code:
void lookAtPlayer()
{
Quaternion rotation =Quaternion.LookRotation(fpsTarget.position-transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation,rotation,Time.deltaTime+damping);
}
The Quaternion LookRotation(forward, upwards) assumes the Z axis will be aligned with forward and the Y axis with upwards if used to set to a transform. From the screenshot, this is what is happening to your object.
Try this overload: Quaternion rotation =Quaternion.LookRotation((fpsTarget.position-transform.position),new Vector3(0,0,1));
Here: forward direction is the direction of the enemy and the upwards direction is the Z axis.
I this doesn't works anyhow, change all the rotations to zero of the parent and try to align the child meshes so that the orientation of your gun is
Y-> upwards Z-> forward
{currently it's the opposite}
PS: Thanks for a screenshot. It made things easier to understand.
Your answer
![](https://koobas.hobune.stream/wayback/20220612120915im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a bubble level (not a game but work tool) 1 Answer
Unity2d shooting bullets is off when the gun is rotated to the left 0 Answers
lookat wont track target 1 Answer