- Home /
Question by
programmincpluslpus · Jul 22, 2021 at 03:45 PM ·
mouseposition2.5d
How do I get a bullet to shoot in the direction of my mouse in a 2.5D game?
I want a bullet (the code for actually summoning the prefab is in another script) to go in the direction of my mouse? I don't want it to follow my mouse either, just get the position once and go in that direction. I'm very new to Unity, so my code is basic.
The code I use spawns the bullet thousands of units away for no reason, even though I got rid of z direction in the mouse Position. Could you help me do it properly?
This is the code I thought would work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScriptForBullet : MonoBehaviour {
public float speed = 25;
// Start is called before the first frame update
void Start() {
Destroy(this.gameObject, 10.0f);
}
// Update is called once per frame
void Update() {
Vector3 direction = Input.mousePosition;
direction.z = 0.0f;
transform.Translate(direction.x * speed * Time.deltaTime, direction.y * speed * Time.deltaTime, direction.z * Time.deltaTime);
}
}
Comment
Can you provide the script you use to instantiate your bullet?
Your answer