Hey guys, devxtreme here, i’m trying to create a game like the brackeys ball wars: Making of BALL WARS - YouTube
so i basically need help with the shooting based on the mouse position
You want to shoot towards the mouse position? That can be done rather easily:
// Copyright © 2021-2022 PrecisionRender. Available under the MIT license.
using System;
using System.Collections.Generic;
using FlaxEngine;
namespace Game
{
public class BulletShoot : Script
{
[Serialize, ShowInEditor]
Prefab Bullet;
public override void OnUpdate()
{
if (Input.GetAction("Fire"))
{
Actor bullet = PrefabManager.SpawnPrefab(Bullet, Actor.Position, Actor.Orientation);
Ray ray = Camera.MainCamera.ConvertMouseToRay(Input.MousePosition);
if (Physics.RayCast(ray.Position, ray.Direction, out RayCastHit hit))
{
hit.Point.Z = Actor.Position.Z;
bullet.LookAt(hit.Point);
}
}
}
}
}
Note: This code is a stripped-down version of the BulletShoot
script from my Top-Down Shooter game. In its current state, this script is untested.
Well, it didn’t work, I think I would check the engine’s Api to find a function to handle the player’s rotation in the direction of the mouse, so I only have to bother about forward shooting
That sounds like a good idea. But the script should’ve worked. I wonder why it didn’t.