Edit: Ok I got the mouse position working and its working good, but the projectile is shooting out the wrong direction. Its shooting out the Z direction, when I need it to shoot out the X.
using UnityEngine;
using System.Collections;
public class Shooter : MonoBehaviour {
public Rigidbody projectile;
public Transform shotPos; //attatched to player
public float shotForce = 1000f;
public float moveSpeed = 10f;
//Mouse Following
private Vector3 mousePos;
public Transform target;
private Vector3 objectPos;
private float angle;
private Transform fixedRotation;
void Update (){
mousePos = Input.mousePosition;
mousePos.z = -20; //distance between camera and object
objectPos = Camera.main.WorldToScreenPoint (target.position);
Debug.Log(mousePos);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
angle = Mathf.Atan2 (mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler (new Vector3(0, 0, angle));
float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
if(Input.GetButtonUp("Fire1")){
Rigidbody shot = Instantiate(projectile, shotPos.position, shotPos.rotation) as Rigidbody;
shot.AddForce(shotPos.forward * shotForce);
}
}
}
Thanks!! any help is appreciated.