Basically what the title says. Im creating a 2D top down shooter with XZ Axis using 2dtoolkit and such. My character has a character controller and uses the follow script that makes the character be able to move and rotate where the mouse is that.
private Vector3 moveDirection = Vector3.zero;
private float distanceFromCamera = 1000.0f;
public float speed;
Vector3 to = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distanceFromCamera));
Quaternion rot = Quaternion.LookRotation(to - transform.position, Vector3.up);
transform.rotation =rot;
//Movement
CharacterController controller = GetComponent<CharacterController>();
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection *= speed;
controller.Move(moveDirection * Time.deltaTime);
Here is my shooting script.
using UnityEngine;
using System.Collections;
public class playerShoot : MonoBehaviour {
public Transform bullet;
public float bulletSpeed;
public float sprayFactor;
public float minLength;
public float maxLength;
public float shotTimer;
public bool canShoot = true;
// Update is called once per frame
void FixedUpdate () {
if(Input.GetMouseButton(0)){
if(canShoot){
var sprayY = Random.Range(-sprayFactor,sprayFactor);
var bulletLength = Random.Range(minLength,maxLength);
var activeBullet = Instantiate(bullet,transform.position,transform.rotation) as Transform;
activeBullet.transform.localScale = new Vector3(.2f,bulletLength,.2f);
activeBullet.transform.Rotate(new Vector3(0f,sprayY,0f));
activeBullet.rigidbody.velocity = transform.TransformDirection(new Vector3(0,0,bulletSpeed));
canShoot=false;
StartCoroutine("shotTime");
}
else{
}
}
}
IEnumerator shotTime(){
yield return new WaitForSeconds(shotTimer);
canShoot=true;
}
}
Now for the problem as the mouse goes farther away from the character the projectile(bullet) doesn’t stay constant and goes really fast. As you move towards the player the projectile goes slower.
I’m still trying to fix this problem and heres my hypothesis. The character rotation and is a slant and you away from the mouse or the projectile’s speed is somehow connected to the distance between the character and the mouse position.
Here is an example webplayer
https://dl.dropboxusercontent.com/u/67760294/Problem3/Desktop.html
Any help would be delightful and thank you in advance.