Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by tanveertak · May 01, 2017 at 01:24 PM · c#lookattransform.rotation

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

alt text

during running this is the problem I I I V

alt text

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;

 }



     






}

during-running.png (388.5 kB)
before-running.png (431.8 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RobAnthem · May 10, 2017 at 05:42 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanveertak · May 15, 2017 at 03:52 PM 0
Share

thanks bro

avatar image Piyush_Pandey tanveertak · May 16, 2017 at 06:44 AM 0
Share

You are welcome

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges