1
\$\begingroup\$

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;
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ What is the desired behaviour? And you might want to translate some of your comments to English as you might get more help :) \$\endgroup\$
    – Vaillancourt
    Commented May 28, 2015 at 17:22
  • \$\begingroup\$ It is about Convert the X and Y Axis to a rotation. There is on every axis a 1 and -1. Somehow i have to convert this in a Rotation-angle. I thought that it would be enough with if() but it doesnt work very well because my character is not moving in the direction that i want. He just moves somehow in this direction but not very precise \$\endgroup\$
    – Nick
    Commented May 28, 2015 at 17:28

2 Answers 2

1
\$\begingroup\$

It sounds like you wish to control the players movement with keys, and aim the weapon with the mouse (hard to tell as you don't provide enough info.). If that is the case then drive the players movement with something simple like :

 //Players Update...
 float speed = 1.0f;

 void Update() {
     var move = Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
     transform.position += move * speed * Time.deltaTime;
 }

And then you could use some mouse rotation control eg:

//Weapon Update...
void Update() {
    RotateToMouse();
}

void RotateToMouse() {
    Vector3 pos = Input.mousePosition;
    pos.z = Mathf.Abs(Camera.main.transform.position.y - transform.position.y);
    pos = Camera.main.ScreenToWorldPoint(pos);
    transform.LookAt(pos);
}
\$\endgroup\$
0
\$\begingroup\$

Use tan-1 ( y/x ) to find the angle to face, using x,y relative from the shooter, then use the Rotation Matrix to find other values you might need.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .