Hi2,
This seems to be an easy problem to fix.,
but i still cannot figure it on my own.
I need to be able to find the position of my mouse click at the 3D plane at world space which corresponding to plane's scale.
For example:
if i click the point a in my game view., it should return me 0,0f
The MainCamera however, can do move around.
and then if click point C it should return me a Vector2 which (i believe) should correspond with the scaling of the 3d object
and clicking inside the 3D plane, should once again return me a Vector2 according to the Object's scaling.
I know that i can use Input.mousePosition but it just return me the mouse position without any relation to the plane object itself.
There is " Camera.ScreenToWorldPoint" that converts to in game position.
here:
https://docs.unity3d.com/2018.4/Documentation/ScriptReference/Camera.ScreenToWorldPoint.html
Related
I have a Cinemachine Freelook camera and i want it similar to Skyrim, where the character's rotation follows where the camera is pointing, but only on the Y axis so that i can move my mouse and look at the character from up and down;
This video demonstrates how MrKrabs can move in 3 dimensions, but won't turn.
I already tried creating a blank object and putting it at the center of the character, then using transform.LookAt(Camera) to make it point to where the camera is, and finally getting the Y value of the object's rotation, inverting it with -transform.position.y and applying it to MrKrabs, but it didn't work: it was jittery and overall just a bad user experience, is there a way to turn the gameobject based on the camera's rotation? (having the camera always watching his back)
Something like this should do the trick.
transform.rotation = Quaternion.Euler(0, Camera.main.transform.eulerAngles.y, 0);
Beware that if your camera is a child of your object it will rotate with it.
Hey team I'm currently working on a 3rd person game where I would like to fire grappling hooks from the player to a point that is offset from the center of the camera.
I have a screen overlay canvas with Ui images for crosshairs. When the left shift is held down the crosshairs move outward from center along the x axis and return to center once shift is released, a bit like crosshair spread in games except I want to trigger the spread via the shift key. These crosshairs are meant to dictate the location the anchors of the grappling hook land, originating from the player and hitting whatever object is directly forward of the crosshairs. (imagine attack on titan ODM gear if you've seen it). I am looking for a way to ray cast from the player to the point forward of these crosshairs while they're offset from the center.
So far I have the grappling system set up and working but am having trouble with the direction parameter when I use the crosshair spread. It separates fine but where the hooks land in relation to the cross hairs are obviously out as I'm trying to use angle calculations at the moment instead of what is forward of these reticles.
I'm basically wondering if it is possible to use these screen overlay UI images to cast forward from or if there's a better way to accomplish the same thing. I have my doubts because they're screen overlay so I imagine their coordinates wont be attached to the camera as they appear.
Thanks in advance for your help.
What I would do is determine the location of the reticleson the screen, then (as Aybe suggested) use ScreenPointToRay or ViewportToRay (depending on if it's easier to get a pixel position or a fractional position of each reticle) to Physics.Raycast that ray from the camera into the scene to find where the rays collide. At this point, you have two world positions the player seems to want to shoot the hooks.:
Vector3 hookTarget1;
Vector3 hookTarget2;
So, now you actually have to fire the hooks - but as you know the hooks aren't being shot from the camera, they are being shot from the player, and they maybe offset by a bit. Let's call the originating points (which may have the same value):
Vector3 hookOrigin1;
Vector3 hookOrigin2;
So, then you can create Rays that originate from the hook origins and point at the targets:
Ray hookShot1 = new Ray(hookOrigin1, hookTarget1 - hookOrigin1);
Ray hookShot2 = new Ray(hookOrigin2, hookTarget2 - hookOrigin2);
Using these rays, you can do another Physics.Raycast if you would like, to confirm that there aren't any trees or other obstacles that are between the player and the target location - and if there are, that may be where the anchor should actually sink:
Vector3 anchorPoint1;
Vector3 anchorPoint2;
The segment between the origin of these rays and these anchor points would be appropriate for rendering the cable, calculating physics for how to move the player, as well as checking for collisions with world geometry that might cause the anchor to release.
I have this 3D game I'm developing
In this game I am a roll of toilet paper
If I press space, I get yeeted towards the cursor
I started using raycasts to do this, but that didn't work out, AT ALL
So, how do I get the toilet paper to jump towards the cursor? (how do I get the direction for where the cursor is)
So from what I understood, you want your game object to move in 3D space to the direction of your cursor, which is in 2D space. You could try to get the game object's world location and use Camera.WolrdToScreenPoint(). This gives you Vector3 where x and y are screen coordinates and z is camera world z coordinate. Then you can get your cursor position with Input.mousePosition. Now you have two points that you can use to calculate direction. (You decide if you want to use camera z position or set it to zero.)
Vector3 payerScreenPos = cam.WorldToScreenPoint(player.transform.position);
Vector3 direction = (playerScreenPos - Input.mousePosition).normalized;
You need to have a reference to the current camera (cam) and to your player.
I'm working with OpenGL (OpenTK library) for a while and now I'd like to select an object using mouse (which I will handle myself I think) and allow its movement along with mouse movement.
I'm looking for an algorithm that could help me translate 2D mouse coordinates to my XZ coordinates in 3D (my Y coordinate is constant and corresponds to my "ground" level unless there's another object at this place, it should place moved object at the top of it - but that I'll also handle myself using collision detection).
What I'm asking here is an algorithm to allow my object follow mouse cursor in relation to camera and projection matrix.
UPDATE
I'm working with OpenGL 3.3+
I'm coding a space-invaders like game, and I want to be able to move the player controlled ship with both mouse and keyboard. There is a speed constant that is being used, when moving the ship with the keyboard. When moving the ship with the mouse, only the position of the ship is changed. When moving the ship with the keyboard, the position of the mouse is also changed, to be consistent with the position of the ship.
The constant I mentioned is not an int value, but a float one. The reason behind this is that I want to add a powerup to increase the speed of the ship, and that may or may not be of type int. Also, I wish to be able to fine tune the speed, if requested by players or by game being to difficult. Problem is, the MouseState gives me a pair of int coordinates, but the position of the ship is a pair of float ones. So herein lies the problem :
Each frame, I need to move the ship to the mouse position (mouse controlled), and each frame I need to move the mouse position to the ship (keyboard controlled). Since the mouse gives me int coordinates, the float ones from the ship's position must be converted to int, which destroyes the idea I described earlier. Is there any way I can force the mouse to use float coordinates, either a workaround in XNA or some other API (DirectX or WINAPI)? I'm also toying with the idea of virtual coordinates and screen coordinates, with conversion between them.
Cheers,
Alex
EDIT : Added code snippet :
if (_keybState.IsKeyDown(Keys.Right))
{
Mouse.SetPosition(
(int)(_player1.GetPlayerPosition().X + Constants.HorizontalMovementSpeed),
(int)(_player1.GetPlayerPosition().Y));
}
if (_keybState.IsKeyDown(Keys.Left))
{
Mouse.SetPosition(
(int)(_player1.GetPlayerPosition().X - Constants.HorizontalMovementSpeed),
(int)(_player1.GetPlayerPosition().Y));
}
if (_keybState.IsKeyDown(Keys.Up))
{
Mouse.SetPosition(
(int)(_player1.GetPlayerPosition().X ),
(int)(_player1.GetPlayerPosition().Y - Constants.VerticalMovementSpeed));
}
if (_keybState.IsKeyDown(Keys.Down))
{
Mouse.SetPosition(
(int)(_player1.GetPlayerPosition().X),
(int)(_player1.GetPlayerPosition().Y + Constants.VerticalMovementSpeed));
}
if (_keybState.IsKeyDown(Keys.Space) && _shootKeyPressed == false)
{
_player1.Shoot();
_shootKeyPressed = true;
}
_shoboState = Mouse.GetState();
_player1.MovePlayerShipToPosition(new Vector2(_shoboState.X, _shoboState.Y));
Instead of tying the mouse and ship positions together like this:
_shoboState = Mouse.GetState();
_player1.MovePlayerShipToPosition(new Vector2(_shoboState.X, _shoboState.Y));
Try something along these lines:
Find unit vector between mouse and ship. This will give you a 1 unit step from your ship to your mouse. (You should be able to find tutorials for this fairly easily)
Multiply the unit vector by your movement amount. This gives you a direction and distance for you to move your ship.
Apply new vector to your ship.
Move the mouse as close as you can to your ship's new position.