Question by
Dan5500 · Jan 20, 2021 at 06:43 PM ·
movementgameobjectmousemouseposition
Making GameObject move in the direction of my mouse (3d)
Ok, so I made 2 different scripts that launches a bullet when you press left click. BUT, the bullet only goes right, all the time. I want the bullet to go in the direction of the mouse when I press left click. Here is the code of the Bullet and the Gun:
Gun:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shoot : MonoBehaviour
{
private Vector3 mousePosition;
public Rigidbody BulletT;
private Vector3 direction;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
float x = Input.mousePosition.x;
float y = Input.mousePosition.y;
Rigidbody BC = Instantiate(BulletT, transform.position, transform.rotation);
}
}
}
Bullet:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public Rigidbody BulletC;
public float speed = 15f;
void Start()
{
BulletC = GetComponent<Rigidbody>();
BulletC.velocity = transform.right * speed;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Enemy")
{
//insert code here
}
else
{
if (other.gameObject.tag == "Player")
{
return;
}
else
{
Destroy(gameObject);
}
}
}
}
Thank you for helping!
Comment