Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Theyrr · Oct 20, 2013 at 03:56 PM · vector3degree

Object faces -180 degrees away

I am building a tower defense game, but with my code, the tower faces -180 degrees away from the target. Why?

 using UnityEngine;
 using System.Collections;
 
 public class Cannon : MonoBehaviour {
     public GameObject projectile;
     public float reloadTime = 1f;
     public float rotateSpeed = 5f;
     public float cooldown = .25f;
     public GameObject muzzleEffect;
     public float errorAmount = .001f;
     public Transform target;
     public Transform[] muzzlePositions;
     public Transform turretBall;
     
     private float nextFireTime;
     private float nextMoveTime;
     private Quaternion desiredRotation;
     private float aimError;
     
     void  Update (){
         if(target) {
             if(Time.time >= nextMoveTime) {
                 CalculateAimPosition(target.position);
                 turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime * rotateSpeed);
             }
             
             if(Time.time >= nextFireTime) {
                 FireProjectile();
             }
         }
     }
     
     void  OnTriggerEnter (Collider other){
         if(other.gameObject.tag == "Enemy") {
             nextFireTime = Time.time + (reloadTime * .5f);
             target = other.gameObject.transform;
         }
     }
     
     void  OnTriggerExit (Collider other){
         if(other.gameObject.transform == target) {
             target = null;
         }
     }
     
     void  CalculateAimPosition (Vector3 targetPos){
         Vector3 aimPoint = new Vector3(targetPos.x + aimError, targetPos.y + aimError, targetPos.z + aimError);
         desiredRotation = Quaternion.LookRotation(aimPoint);
     }
     
     void  CalculateAimError (){
         aimError = Random.Range(-errorAmount, errorAmount);
     }
     
     void  FireProjectile (){
         //audio.Play();
         nextFireTime = Time.time + reloadTime;
         nextMoveTime = Time.time + cooldown;
         CalculateAimError();
         
         foreach(Transform theMuzzlePos in muzzlePositions) {
             Instantiate(projectile, theMuzzlePos.position, theMuzzlePos.rotation);
             Instantiate(muzzleEffect, theMuzzlePos.position, theMuzzlePos.rotation);
         }
     }
 }
Comment
Add comment
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

1 Reply

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

Answer by robertbu · Oct 20, 2013 at 04:00 PM

Quaternion.LookRotation() takes a vector, not a position. I think you want:

 Quaternion.LookRotation(aimPoint -  turretBall.position);
Comment
Add comment · Show 6 · 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 Theyrr · Oct 20, 2013 at 04:48 PM 0
Share

Now it does not rotate at all. I got the following error:

transform.rotation assign attempt for 'Sphere' is not valid. Input rotation is { NaN, NaN, NaN, NaN }.

avatar image robertbu · Oct 20, 2013 at 07:31 PM 0
Share

You might get this if the aimPoint and turretBall.position are the same position. You can't really look at something that is at the same position you are. Though this is must a guess since I don't have the actual code and error. But you might fix it like this:

 if (aimPoint != turretBall.position) {
     desiredRotation = Quaternion.LookRotation(aimPoint - turretBall.position);
 }
avatar image Theyrr · Oct 21, 2013 at 11:00 AM 0
Share

Then we are back to the starting position. I'll leave the actual code at the current state in the bottom since you said you don't have it. I could also give you my project files if you want to take a deeper look in it. It also rotates now, but still faces the -180 degrees away.

 using UnityEngine;
 using System.Collections;
 
 public class Cannon : $$anonymous$$onoBehaviour {
     public GameObject projectile;
     public float reloadTime = 1f;
     public float rotateSpeed = 5f;
     public float cooldown = .25f;
     public GameObject muzzleEffect;
     public float errorAmount = .001f;
     public Transform target;
     public Transform[] muzzlePositions;
     public Transform turretBall;
     
     private float nextFireTime;
     private float next$$anonymous$$oveTime;
     private Quaternion desiredRotation;
     private float aimError;
     
     void  Update (){
         if(target) {
             if(Time.time >= next$$anonymous$$oveTime) {
                 CalculateAimPosition(target.position);
                 turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime * rotateSpeed);
             }
             
             if(Time.time >= nextFireTime) {
                 FireProjectile();
             }
         }
     }
     
     void  OnTriggerEnter (Collider other){
         if(other.gameObject.tag == "Enemy") {
             nextFireTime = Time.time + (reloadTime * .5f);
             target = other.gameObject.transform;
         }
     }
     
     void  OnTriggerExit (Collider other){
         if(other.gameObject.transform == target) {
             target = null;
         }
     }
     
     void  CalculateAimPosition (Vector3 targetPos){
         Vector3 aimPoint = new Vector3(targetPos.x + aimError, targetPos.y + aimError, targetPos.z + aimError);
         if (aimPoint != turretBall.position) {
             desiredRotation = Quaternion.LookRotation(aimPoint - turretBall.position);
         }
     }
     
     void  CalculateAimError (){
         aimError = Random.Range(-errorAmount, errorAmount);
     }
     
     void  FireProjectile (){
         //audio.Play();
         nextFireTime = Time.time + reloadTime;
         next$$anonymous$$oveTime = Time.time + cooldown;
         CalculateAimError();
         
         foreach(Transform the$$anonymous$$uzzlePos in muzzlePositions) {
             Instantiate(projectile, the$$anonymous$$uzzlePos.position, the$$anonymous$$uzzlePos.rotation);
             Instantiate(muzzleEffect, the$$anonymous$$uzzlePos.position, the$$anonymous$$uzzlePos.rotation);
         }
     }
 }
avatar image robertbu · Oct 21, 2013 at 03:11 PM 0
Share

The code here looks right. Are you sure your model is constructed correctly? The "front" needs to be facing positive 'Z' when the rotation is (0,0,0). If it is totally 180 degrees off, you can "fix" the problem by reversing the parameters:

 desiredRotation = Quaternion.LookRotation(turretBall.position - aimPoint);

But I'd make sure you figure out the underlying problem before making the change. If you want to give me a link to a package or a zip file of the project, I'll take a look.

avatar image Theyrr · Oct 21, 2013 at 03:29 PM 0
Share

Reversing did it. Thank you.

Show more comments

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

15 People are following this question.

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

Related Questions

is there any code that will rotate a vector3 by an angle? 1 Answer

Use yAxis as a Vector3(0,0,yAxis) paramenter? 3 Answers

Relative Rotation 1 Answer

Need some help with to grap .x with a Raycasthit (c#) 1 Answer

Unable to fully rotate GameObject on tap 2 Answers


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