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
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);
Works PERFECTLY ! THAN$$anonymous$$ YOU very, very much! :)
Your answer
Follow this Question
Related Questions
Boss ai help EoW 0 Answers
Enemies cannot kill Player. 0 Answers
A 2D Character Controller with Rigidbody2D.AddForce 2 Answers