- Home /
Question by
chinjl · Mar 12, 2018 at 01:58 AM ·
c#shootingmousepositionmouseclick
The shooter wont shoot towards mouse position in unity3d
every time i shoot, the place that shoot out the bullet will be the same position, it wont shoot according the mouse position.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shoot : MonoBehaviour {
public GameObject bullet;
public Transform firePoint;
public float fireRate = 3f;
public float speed;
private Vector3 mousePos;
private Vector3 aims;
void Awake()
{
}
void Update ()
{
Vector3 mousePositionVector3 = new Vector3(Input.mousePosition.x,Input.mousePosition.y,0);
mousePositionVector3 = Camera.main.ScreenToWorldPoint(mousePositionVector3);
Vector3 targetdir = mousePositionVector3 - transform.position;
transform.rotation= Quaternion.LookRotation(Vector3.forward,targetdir);
if (Input.GetMouseButtonUp (0)) {
//isFiring = true;
//Shooting ();
// Ray cameraRay = Camera.main.ScreenPointToRay (Input.mousePosition);
// Plane groundPlane = new Plane (Camera.main.transform.forward, Vector3.zero);
// float rayLength;
// if (groundPlane.Raycast (cameraRay, out rayLength)) {
// Vector3 pointToLook = cameraRay.GetPoint (rayLength);
// Debug.DrawLine (cameraRay.origin, pointToLook, Color.blue);
// Rigidbody rocketInstance;
// rocketInstance = Instantiate (bullet, firePoint.position, firePoint.rotation) as Rigidbody;
// rocketInstance.transform.LookAt (pointToLook);
// rocketInstance.AddForce (firePoint.transform.right * 500);
GameObject newBullet = Instantiate(bullet, transform.position, transform.rotation) as GameObject;
Rigidbody rigid = newBullet.GetComponent<Rigidbody>();
rigid.velocity = transform.up * 10;
}
}
}
// void Shooting()
// {
// Rigidbody rocketInstance;
// rocketInstance = Instantiate (bullet, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
// rocketInstance.AddForce (barrelEnd.right * 500);
// }
screenshot-3.png
(131.1 kB)
Comment
Your answer