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 Redbobster · Jan 10, 2017 at 09:43 PM · rotation2d gamequaternionsprites

How do I get the sprite pointing the right direction?

I am busy making a 2D tank game and it was coming along nicely until I hit a road block, I've completed a line of code for my enemy AI which allows them to lead their shots, this line of code works but the sprite is turned on its side so you can't it or the bullets it creates, here is the code:

using UnityEngine; using System.Collections;

public class Enemy_Turret : MonoBehaviour {

 //values that will be set in the Inspector
 public GameObject Target;
 public float RotationSpeed;
 public float reload;
 public float ReloadTime;
 public GameObject Bullet;
 public float projectileSpeed;
 public float turnSpeed;

 //values for internal use

 private float distTarget;
 private float velocityDist1;
 private float velocityDist2;
 private Vector3 velocityPosition1;
 private Vector3 velocityPosition2;
 private Vector3 TargetInterceptPosition;
 private Quaternion rotationTarget;
 Vector3 _prevPosition;

 public void Start()
 {
     transform.rotation = Quaternion.AngleAxis(90f,Vector3.up);
     Target = GameObject.FindWithTag("Friendly");
 }

 // Update is called once per frame
 void FixedUpdate()
 {
     //finds velocity of the target
     Vector3 vel = (Target.transform.position - _prevPosition) / Time.deltaTime;
     _prevPosition = Target.transform.position;

     //allows the ai to lead shots rather than shooting at the targets current position
     if (Target)
     {
         distTarget = Vector3.Distance(Target.transform.position, transform.position);
         velocityPosition1 = Target.transform.position + ((vel) * (distTarget / projectileSpeed));
         velocityDist1 = Vector3.Distance(velocityPosition1, transform.position);
         velocityPosition2 = Target.transform.position + ((vel) * (velocityDist1 / projectileSpeed));
         velocityDist2 = Vector3.Distance(velocityPosition2, transform.position);
         TargetInterceptPosition = Target.transform.position + ((vel) * (velocityDist2 / projectileSpeed));
         rotationTarget = Quaternion.LookRotation((TargetInterceptPosition) - transform.position);
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(rotationTarget.eulerAngles.x + 90, rotationTarget.eulerAngles.y, rotationTarget.eulerAngles.z), Time.deltaTime * RotationSpeed);
         Debug.Log(Target);
     }
     else
     {
         Target = GameObject.FindWithTag("Friendly");//or find closest object in seperate function, etc.
     }
 

     if (reload < 0)
     {
         Instantiate(Bullet, transform.position, transform.rotation);
         reload = ReloadTime;
     }
     reload = reload - 1;
 }


}

if you could help me fix this I would be very thankful, I've been scouring the internet for the past week and trying different solutions but nothing has worked

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
1
Best Answer

Answer by Redbobster · Jan 13, 2017 at 08:32 AM

Hey guys, fixed the problem, just took a different approach to the rotation of the turret, all the calculations for leading the shot works and the AI is now laser accurate while you move in a straight line. Here's the code for those who are having similar issues or just need a code for leading shots:

using UnityEngine; using System.Collections;

public class Enemy_Turret : MonoBehaviour {

 //values that will be set in the Inspector
 public GameObject Target;
 public float RotationSpeed;
 public float reload;
 public float ReloadTime;
 public GameObject Bullet;
 public float projectileSpeed;

 //values for internal use

 private float distTarget;
 private float velocityDist1;
 private float velocityDist2;
 private Vector3 velocityPosition1;
 private Vector3 velocityPosition2;
 private Vector3 TargetInterceptPosition;
 Vector3 _prevPosition;

 public void Start()
 {
     Target = GameObject.FindWithTag("Friendly");
 }

 // Update is called once per frame
 void FixedUpdate()
 {
     //finds velocity of the target
     Vector3 vel = (Target.transform.position - _prevPosition) / Time.deltaTime;
     _prevPosition = Target.transform.position;


     if (Target)
     {//allows the ai to lead shots rather than shooting at the targets current position
         distTarget = Vector3.Distance(Target.transform.position, transform.position);
         velocityPosition1 = Target.transform.position + ((vel) * (distTarget / projectileSpeed));
         velocityDist1 = Vector3.Distance(velocityPosition1, transform.position);
         velocityPosition2 = Target.transform.position + ((vel) * (velocityDist1 / projectileSpeed));
         velocityDist2 = Vector3.Distance(velocityPosition2, transform.position);
         TargetInterceptPosition = Target.transform.position + ((vel) * (velocityDist2 / projectileSpeed));
          Vector3 vectorToTarget = TargetInterceptPosition - transform.position;//finds out where the enemy is in relation to target
         float angle = (Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg) - 90;//finds the angle the turret needs to turns towards
         Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
         transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * RotationSpeed);//turns the turret towards the target A.K.A player
     }
     else
     {
         Target = GameObject.FindWithTag("Friendly");//or find closest object in seperate function, etc.
     }
 

     if (reload < 0)
     {
         Instantiate(Bullet, transform.position, transform.rotation);
         reload = ReloadTime;
     }
     reload = reload - 1;
 }


}

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rotation of Player isn't very accurate 0 Answers

Counting a 2D objects flips, full 360 rotation on the Z axis. C# 0 Answers

How do I make a 2D sprite smoothly rotate upright? 1 Answer

How do I rotate a 2D object based on it's velocity? 1 Answer

Sword rotation not working? 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