So my Problem is that my Character is always shooting in the way it is looking. Unfortunaly my solution seems to be a very bad one. I am not really cabable of aiming with my solution. Neither with Controller or Touchscreen Control. I already commented many Things out as you can see but nothing works correctly for me. It is a 2D Top-Down Game.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public int dead = 0;
//Moving
public float maxSpeed = 3f;
public float rotSpeed = 100f;
public Transform target;
float zo;
float opZ;
//Shooting
public Vector3 bulletOffset = new Vector3(0, 0.5f, 0);
public GameObject bulletPrefab;
int bulletLayer;
public float fireDelay = 0.25f;
float cooldownTimer = 0;
GameObject pau;
void Start () {
bulletLayer = gameObject.layer;
pau = GameObject.FindGameObjectWithTag("MainCamera");
}
// Update is called once per frame
void Update () {
//Debug.Log("GameP ->" + pau.GetComponent<PauseGame>().isPause);
if(!pau.GetComponent<PauseGameGameover>().isPause){
if(dead == 0)
{
//Moving
movePlayer();
//Shooting
cooldownTimer -= Time.deltaTime;
if( CFInput.GetButton("Fire1") && cooldownTimer <= 0 ) {
shotPlayer();
}
}
}
}
void shotPlayer()
{
// SHOOT!
cooldownTimer = fireDelay;
Vector3 offset = transform.rotation * bulletOffset;
GameObject bulletGO = (GameObject)Instantiate(bulletPrefab, transform.position + offset, transform.rotation);
bulletGO.layer = bulletLayer;
}
void movePlayer()
{
float x = CFInput.GetAxis("Horizontal");
float y = CFInput.GetAxis("Vertical");
////////////////
/// Rotate the PLAYER
/// /////////////////////////////
zo= x*90;
//Debug.Log("ZO -> " + zo);
if(y > 0 && x > 0){opZ = 360-zo;} //Oben bis Rechts
if(y < 0 && x > 0){opZ = 180+zo;} // Rechts bis unten
if(y < 0 && x < 0){opZ = 180+zo;} //Unten bis Links
if(y > 0 && x < 0){opZ = -1*zo;} // Links bis oben
/* Hardcoded Left Right Up Down */
/*if(y == 0 && x > 0){opZ = 270;}
if(y == 0 && x < 0){opZ = 90;}
if(y < 0 && x == 0){opZ = 180;}
if(y > 0 && x == 0){opZ = 360;}
if(y == 0.5 && x == 0.5){opZ = 315;}
if(y == -0.5 && x == 0.5){opZ = 225;}
if(y == -0.5 && x == -0.5){opZ = 135;}
if(y == 0.5 && x == -0.5){opZ = 45;}
//Debug.Log("OPZ: ->" + opZ);
*/
// Grab our rotation quaternion
Quaternion rot = transform.rotation;
// Grab the Z euler angle
//float z = rot.eulerAngles.z;
// Recreate the quaternion
Quaternion rotNew = Quaternion.Euler( 0, 0, opZ );
transform.rotation = Quaternion.Lerp(rot, rotNew, Time.time * rotSpeed);
///////////////////
// MOVE the PLAYER
////////////////////////
///
Vector3 pos = transform.position;
Vector3 velocity = new Vector3(CFInput.GetAxis("Horizontal") * maxSpeed * Time.deltaTime, CFInput.GetAxis("Vertical") * maxSpeed * Time.deltaTime, 0);
pos += velocity;
// Finally, update our position!!
transform.position = pos;
}
}