Unity: Ballistic Vector - vector

This is a first person shooter game where I am trying to launch a projectile from a moving player. The launch direction depends on where i click on the screen. The "launcher" has a fixed fire strength, which means that the projectile if fired more horizontally will travel further before hitting the ground, likewise a projectile fired in a more upwards direction will go higher but will have travelled less horizontally when it hits the ground due to gravity. The firing vector is determined by where the finger touches the screen, and then multipled by a public parameter "firingstrength". Make sense so far?
What I am confused about is how to get the position of the finger on the screen, which i use to calculate the "vector" to apply to the projectile.
I have imagined doing this (I am new to Unity) by the following:
Empty object (Player): Contains
movement scripts
Camera
Invisble inverted sphere which surrounds the camera, and I use this sphere to pick up the mouse clicks (i.e when i click on the screen the game should detect where on the inside of the sphere I have clicked on, in order to calculate a vector between the camera position and the sphere wall that i clicked on)
Once i have the vector, i just multiply it by a "firingstrength variable and apply it to a projectile that originates from camera position.
Does this make sense or is there a better way to do this?
Kevin

You do not need a sphere just use Input.mousePosition and construct a ray from it using ScreenPointToRay(). Than you just use ray.Direction to get Vector3 from your camera position.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 shootVector = ray.Diraction;
This should do the trick.
For touch interface:
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
Vector3 shootVector = ray.Diraction;

Related

Get an offset behind player forward without target

I need a constant offest behind the player forward based on player rotation and player position. So if the player rotate or move this offset is always at a costant position behind the player forward. For example if this variable has the value 10 i get the player position 10 meters behind his current position. Any idea? Target info no needed. Thanks in advance.

Aframe Physics system: how to stop a dynamic-body at a point

My use-case is to let a ball bounce and come towards the camera (been able to do that with a simple dynamic-body sphere on a static-body grid). However, rather than it rolling down to a position where it loses its velocity (or momentum), is there a way to stop it at a desired point? I tried placing a (invisible) hurdle object but it rolls back. I would like it to remain stationary once it reaches the desired point. Thanks
You can stop the dynamic-body by zeroing a few body attributes
let body = el.body // el = aframe entity
body.velocity.set(0,0,0);
body.angularVelocity.set(0,0,0);
body.vlambda.set(0,0,0);
body.wlambda.set(0,0,0);
Working example here.
However IF your ball is on a slope, the physics engine will, properly, slowly accelerate it.
If you want it to stop no matter what (defying the laws of CANNON.js physics), then either remove the dynamic-body component, or swap it with a static-body.

JavaFX 2d game engine: How to move camera as player moves?

I am creating a 2d topdown game (with canvas size 720x480) where I want the camera to move once the player reaches a certain part of the screen. For example, once the player reaches 2/3 of the way to the right, the camera should then start scrolling to the right.
For reference, here is an image of the game world: game
In my code, I have every object implementing a "genericObj" class, which has a position, velocity, and dimensions. So this is what I am thinking of doing once the player reaches 2/3 of the way to the right and is continuing to move to the right:
set the player's velocity to half the original
update every object's velocity with the negative of half the player's velocity (object.velocity -= player.velocity)
check if objects are within the view of the camera
display the objects that are within the view, disregard others
The reason for using half the player's velocity for both the new player velocity and the objects is that, in my code, the player movement sprite is only updated when the player is moving. Therefore, I need the player to be moving as opposed to setting the player velocity to 0 and the velocity of every object to negative player.velocity.
Is this a good way of "moving" the camera? What are some better methods of moving the camera? Also, would performance be an issue if I used this method of moving the camera (for example if I had 50+ objects)?

Is it posible get distance of object from picture with static camera?

I am drawing situation.
Camera (Raspberry pi) looking on scene, there is an object. I know real width and height of object. Is there any way, how can I calculate distance between camera and object from cam photo? Object on picture is not always situated in the middle. I know height of camera and angle of camera.
Yes, but it's gonna be difficult. You need the intrinsic camera matrix and a depth map. If you don't have them, then forget it. If you only posses a RGB image then it won't work.

2d game rotation interpolation over network

I am working on a simple 2d multiplayer game with players that have a turret that faces a certain direction (0-360 degrees)
The turret is slow at changing its direction and thus has two values:
heading, which is the direction it is currently facing
desiredHeading, which is the direction it wants to face (player input)
The function moveTurret() is called once per frame and increases/decreases the heading by a certain value, so it matches the desiredHeading after some time. (--> turret moves slowly into desired direction)
Since my client receives updates every X milliseconds via the network, I have a hard time to render those rotations smoothly!
My current approach is as follows:
client periodically receives the heading for each other player
client sets the received values as desiredHeading for the respective players
moveTurret() is called for those players every frame
I thought the client would then approximate the rotation and would smoothly render the turret into its right direction, however this doesn't work at all and the animation is wobbling back and forth.
Does anyone know a simple approach for this problem? I have found some quaternion interpolation and matrix based algorithms, but none seemed to fit for my approach, since I only have a heading-value and my game does not use matrix calculations.

Resources