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 LethianGames · May 25, 2016 at 10:17 AM · 2dplayerenemytop downlockon

Turning player toward enemy after bool is true/button is pressed

So, I need my player to still be movable but turn towards an enemy while holding down a button - kinda like a lock-on. But this all has to be in a 2D environment (It's a top down brawler). Here's the code part where i need to declare my "heading":

 float heading = Mathf.Atan2(movementVector.x*(-1), movementVector.y*(-1));
 
         if (playerLockOn == true)
         {
             transform.rotation = ///NEED IDEAS HERE///;
         }else{
 
         transform.rotation = Quaternion.Euler (0f,0f,heading*Mathf.Rad2Deg);
         }
 

Any ideas on how to go on about this? I'll post my full code down below:

 using UnityEngine;
 using System.Collections;
 
 public class Player_Movement : MonoBehaviour 
 {
 
     private Vector2 movementVector;
 
     private float movementSpeed = 5;
     // Use this for initialization
 
     private float heading;
     private Quaternion rotationToHold;
 
     private bool movingX;
     private bool movingY;
 
     public bool wallTrigger;
     public bool playerLockOn;
 
     public Animation anim;
 
     public Transform Enemy;
 
     void Start () 
     {
         anim = GetComponentInChildren<Animation> ();
         Enemy = GameObject.Find ("Test Enemy").transform;
     }
 
 
 
     // Update is called once per frame
     void Update () 
     {
         //Checks, if player attacks and does animation
         if (Input.GetKeyDown ("joystick button 2")) {
             anim.Play ();
         }
 
         //Player Locking on to enemy
         if (Input.GetKey ("joystick button 0")) {
             playerLockOn = true;
         } else {
             playerLockOn = false;
         }
             
 
         movementVector.x = Input.GetAxis ("LeftJoystick_X") * movementSpeed;
         movementVector.y = Input.GetAxis ("LeftJoystick_Y") * movementSpeed;
         ///Moving the Player
         GetComponent<Rigidbody2D>().velocity = new Vector2 (movementVector.x, movementVector.y*(-1));
 
         ///turning the player and checking if defending for lockOn
 
         float heading = Mathf.Atan2(movementVector.x*(-1), movementVector.y*(-1));
 
         if (playerLockOn == true)
         {
                           transform.rotation = //???//
         }else{
 
         transform.rotation = Quaternion.Euler (0f,0f,heading*Mathf.Rad2Deg);
         }
 
         //movement checks in order to keep rotation after letting go of stick
         if (movementVector.x == 0)
         {
             movingX = false;
         }
         else{
             movingX = true;
         }
 
         if (movementVector.y == 0) {
             movingY = false;
         } else {
             movingY = true;
         }
 
 
         if (movingX || movingY == true) {
             rotationToHold = transform.rotation;
         } else {
             transform.rotation = rotationToHold;
         }
 
         ///check if Player is inside the arena and restric his movement if not
         if (wallTrigger == true) {
             movementSpeed = 0;
         } else {
             movementSpeed = 5;
         }
     }
 
     void OnTriggerEnter2D (Collider2D other)
     {
         wallTrigger = false;
     }
 
     void OnTriggerExit2D (Collider2D other)
     {
         wallTrigger = true;
 
         var magnitude = 2500;
 
         var force = transform.position - other.transform.position;
 
         force.Normalize ();
         GetComponent<Rigidbody2D> ().AddForce (-force * magnitude);
 
 
     }
 }


Thank you guys and gals for any help!

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 BackslashOllie · May 25, 2016 at 10:44 AM

Try something like this in your if playerLockOn statement (Untested):

 Vector3 diff = Enemy.position - transform.position;
 diff.Normalize();
 
 float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
 transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90);
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 LethianGames · May 25, 2016 at 11:43 AM 0
Share

Works PERFECTLY ! THAN$$anonymous$$ YOU very, very much! :)

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

87 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

Related Questions

My enemy doesn't die when my player attacks, when I try to click space bar, the player is hitting the enemy, but the enemy only acknowledges the damage after five or more hits, any advice? 1 Answer

Boss ai help EoW 0 Answers

Enemies cannot kill Player. 0 Answers

A 2D Character Controller with Rigidbody2D.AddForce 2 Answers

distinguish players for collision between them 0 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