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 /
  • Help Room /
avatar image
0
Question by Rasgard · Jun 26, 2016 at 01:48 PM · 2drotationmovementcollidervector

2D Top Down, Enemy facing player slow rotation issue. Vector gets messed up after collision.

Hello everyone, the following is the code below for my top down shooter for the enemy. However I found a weird rotation issue where if I use the player to collide with the enemy, then the enemy has its rotation vector messed up. In a way if I spin the enemy somehow through colliding with it, then the enemy will face the player sideways and will keep trying to look at the player sideways instead of face forward. When the enemy gets close and is able to collide with the player on its own the vector seems to back to normal and the enemy will face forward again.

I feel like the main issue is in Update() part of the code where the Slerp and various other calculations are happening.

 using UnityEngine;
 using System.Collections;
 
 public class EnemyScript: MonoBehaviour {
 
     public float EnemyHealth = 100;
     public float speed;
     public float Rspeed;
     public Transform player;
     public Transform target;

     void Update() {
 
         Vector3 vectorToTarget = target.transform.position - transform.position;
         float z = Mathf.Atan2 ((player.transform.position.y - transform.position.y), (player.transform.position.x - transform.position.x)) * Mathf.Rad2Deg - 90;
         Quaternion q = Quaternion.AngleAxis(z, transform.forward);
         transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * Rspeed);
 
 
     }
 
     void FixedUpdate () {

         GetComponent<Rigidbody2D>().AddForce (gameObject.transform.up * speed);
 
     }
   
 
 
 }

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

3 Replies

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

Answer by Rasgard · Jun 27, 2016 at 05:40 AM

I think I figured it out, my Rigidbody2D had 0 Angular Drag. Combine that with no gravity, I had physics forces constantly affecting the object when I hit it, thus causing it to go crazy and face me with its wrong sides. Thank you ChrisAngelMindmapfreak.

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
avatar image
0

Answer by Mindmapfreak · Jun 26, 2016 at 02:17 PM

Why is your enemy moving along the its up-axis and rotating around its forward-axis? I would except it to move along forward and rotate around up.

Anyhow: It is probably easier to use Quaternion.LookRotation than calculate the values yourself. The following code should do the same as your code:

 Vector3 toPlayerVec = player.transform.position - this.transform.position;
 toPlayerVec.z = 0.0f;
 Quaternion q = Quaternion.LookRotation(toPlayerVec, Vector3.forward)*Quaternion.Euler(270,180,0);
Comment
Add comment · Show 1 · 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 Rasgard · Jun 26, 2016 at 02:24 PM 0
Share

I just simply had to change the axis so that based on the sprite it would face the right way without editing all the sprite models.

avatar image
0

Answer by Rasgard · Jun 26, 2016 at 04:02 PM

Could someone please modify the script so that I could test it for the bug. Thank you.

Comment
Add comment · Show 4 · 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 Mindmapfreak · Jun 26, 2016 at 07:13 PM 0
Share

I am not sure what you mean. Did replacing

 float z = $$anonymous$$athf.Atan2 ((player.transform.position.y - transform.position.y), (player.transform.position.x - transform.position.x)) * $$anonymous$$athf.Rad2Deg - 90;
          Quaternion q = Quaternion.AngleAxis(z, transform.forward);

with the code I posted make a difference?

avatar image Rasgard Mindmapfreak · Jun 27, 2016 at 03:45 AM 0
Share

Thanks for your post however this does insta rotation. I needed it to rotate slowly towards the player. I had a similar script before that while it does not have weird bugs it is not what I am looking for exactly.

avatar image Rasgard Mindmapfreak · Jun 27, 2016 at 04:41 AM 0
Share

Sorry I just did not realize what to replace exactly. The code works however upon collision the vector still gets messed up a little. So the enemy would try to face me sideways sometimes. This is more noticeable with slow rotation in place.

avatar image Rasgard · Jun 27, 2016 at 05:40 AM 0
Share

I think I figured it out, my Rigidbody2D had 0 Angular Drag. Combine that with no gravity, I had physics forces contantly affecting the object when I hit it, thus causing it to go crazy and face me with its wrong sides. Thank you ChrisAngel$$anonymous$$indmapfreak.

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

105 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 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 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 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 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 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 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

Confusion trying to set position at an angle 1 Answer

Cell-Like Movement on a 2D Simulation: How to stop erratic movement and add colliders 0 Answers

collision is making its own vector 1 Answer

Check if Dump's velocity is zero 0 Answers

Rotating in Z-Axis while still moving using rigidbody2D.velocity,Rotating in z axis while still moving using rigidbody2d 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