What happens when I set the transform.up value in unity. Specifically could you explain this video in the link below
https://youtu.be/_XdqA3xbP2A
I know it changes what the object is looking at. But, does it look at a position, or how does it
work? What happens when I change the direct transform.up? I can’t seem to find a good explanation. Why does the vector in the video make a direction that is facing the mouse, why not use the mousePosition itself instead of subtracting?
Aside from that could you also explain what is going on in the video?
Thanks in advance!
Basically, when you move an object, you take its position and rotation as reference, so if you increment +1 on the Y position of an object and this object is rotated, you'll increment on that direction.
but with Vector3.up, you use the world space as reference, so
i want to give you a full answers here, but i would have to enter with some math, simplifying, the transform.up is the direction which the sprite is pointing, he gets the direction saying this:
"mousePosition.y" (GREEN) is the position Y of the mouse, and "mousePosition.x" (RED) is the X position of the mouse, with this two values, you have a coordinate, or a direction (Blue traced line), which unity transforms to quaternions and apply the value to your rotation (this in 2d). you want to use the Vector3 which uses a world space reference, because if you use a local reference, would happen something like this
and you would get the wrong direction.
Transform object automatically calculates a quaternion rotation using this up Vector like from default Vector3.up to new transform up vector.
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.
So, I'm working on Unity project. I have a mortar, which is leaned towards player gameobject. I want to rotate it so it follows player movement.
I came up with this simple code in my Update method:
Vector3 directionToFace = _player.position - transform.position;
directionToFace.y = 0;
Debug.DrawRay(transform.position, directionToFace, Color.green); // for visualization
transform.rotation = Quaternion.LookRotation(directionToFace);
But setting y to 0 caps rotation of x axis to 0 but I want my mortar to be leaned forward a bit. I've tried setting y to many different values but then distance between two objects determines how much the mortar leans. Yes, I know that I can make it child of an empty gameobject and apply script to the parent but if there is any other way to fix this problem inside my code... Your help will be appreciated.
The only idea I have is to calculate distance between two gameobjects and change value of directionToFace.y accordingly. But I will have to get local rotation that's assigned to mortar in the inspector and then calculate the value using Pythagorean Theorem... but I don't want to torture myself that much :D
Hope I understand your problem! I found it was possible to lean your gameObject forward by adding in this line after your code
transform.rotation = Quaternion.Euler(xOffset,transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);
where xOffset is the amount you want your game object leaning, it would also be possible to add an explicit value for z in a similar way.
Let me know if this isn't what you meant.
I am coding for a game and I have it running fine and all, but I ran into the issue of projectiles. I have no idea of how to calculate the mouse position and send a projectile over. I've looked though many stackoverflow tutorials and many youtube videos, but they are either too vague or very very complex. Could someone please explain the process? I have been coding in C# for 2 years now, so I do have background knowledge. Please Help!
Well, you don't tell what kind of game you are working on and what the projectile you want to "send over", whatever that means but here are a few pointers, maybe one will help:
#1: If you are doing a 2D game, and you want to send a projectile from some point toward the mouse cursor, then you simply get the position of the mouse by calling GetState() on your MouseState instance, and reading it's X and Y properties, then get the direction of the projectile by subtracting the location of the origin of the projectile from the mouse position. Then you can move the projectile along that line by storing the time you launched it, subtracting that every frame from the actual time to get how long it has been on its way and then adding that many times it's speed times the normalized direction to its origin coordinates.
Example:
For this I assume you have a Projectile class that has Vector2 field called origin, one called direction, one called position, a float called speed and an int called startTime and you have an instance of it with origin and speed already set. Modify as necessary for your actual code.
MouseState ms = new MouseState();
in your Update method:
ms.GetState();
projectile.direction = new Vector2(ms.X, ms.Y) - projectile.origin;
projectile.direction.Normalize();
projectile.position = projectile.origin + projectile.speed * (gameTime.TotalGameTime.TotalMilliseconds - projectile.stratTime) * projectile.direction;
And then in the Draw method you just draw the projectile to the coordinates in projectile.position.
#2: You are doing a 3D game and want to shoot a projectile originating from your camera and directed toward whatever your mouse is pointing at. You will need to generate a Ray with something like this:
private Ray getClickRay(Vector2 clickLocation, Viewport viewport, Matrix view, Matrix projection)
{
Vector3 nearPoint = viewport.Unproject(new Vector3(clickLocation.X, clickLocation.Y, 0.0f), projection, view, Matrix.Identity);
Vector3 farPoint = viewport.Unproject(new Vector3(clickLocation.X, clickLocation.Y, 1.0f), projection, view, Matrix.Identity);
Vector3 direction = farPoint - nearPoint;
direction.Normalize();
return new Ray(nearPoint, direction);
}
From there you can get the position of your projectile the same way as above, only using Vector3-s instead of Vector2-s.
#3: You want to simulate some sort of launcher and do a physical simulation on the trajectory of the projectile. Since I find this less likely than the first two I'll just describe the idea: you set a minimum and maximum rotation for your launcher along 2 axes, link each rotation to the ratio of the full width and length of your screen to the mouse position, and then use the angles and whatever other variables you have to calculate the direction and speed of the projectile at launch, then simulate physics each frame.
I hope one of these will help.
I'm making game that must set target number on all enemy on screen.
I FAIL TO FIND function like IsInsideCameraView(gameObject)
Now I'm trying to scan area that camera looks.
For this, I need Camera angle width, angle hight.
As described in this emage (My reputation is not enough to show image directly)
My question is similer to this question, How to get angles value of perspective camera in Three.js? but for Unity.
If you know
HOW TO CALCULATE CAMERA WIDTH&HEIGHT (following screen size when unity starts)or
IS THERE GOOD FUNTIONS LIKE IsInsideCameraView(Camera, gameObject)
any answers will be welcomed.
[Untested. Javascript] You can use renderer.isVisible to determine if the object is within the camera frustum.
I FAIL TO FIND function like IsInsideCameraView(gameObject)
You can use:
object.Renderer.isVisible
...to check if the object is visible in any camera.
To test for a specific camera, use:
Vector3 screenPos = camera.WorldToScreenPoint(target.position);
Then check if X and Y coordinates are between (0,0) and Screen.width, Screen.height.
HOW TO CALCULATE CAMERA WIDTH&HEIGHT
That's what Camera.pixelWidth and Camera.pixelHeight are for or you can use Screen.width, Screen.height for cameras that represent the entire screen.
Anyway, that's arguably a XY problem, refer to the beginning of my answer for direct solution.
Tell me more
Is target in view frustum?
I have a Camera Player in a scene. 3D Scene with 2d Compass GUI. Scene contains a fixed north which i want to indicate on my compass with needle but how can i do it?
I tried this code using a Tutorial so far but my needle movement direction is not appropriate according to my north.
public void ChangeNortDirection() {
northDirection.z = player.eulerAngles.y;
northLayer.localEulerAngles = northDirection;
}
You are setting the north direction to the y axis of the player, so if the player rotates so does the north direction.
What you want to do is have a variable indicating the absolute north direction and then just rotate the compass in the opposite direction the player has rotated.
Another thing is, is this in 2D or 3D space? In 2D you only want to rotate the z axis at all times and not touch the x and y axis. And in 3D the north direction should be defined in only the z axis so that only the x and y plane rotates.
For a definitive answer please provide more information.
After looking at the tutorial and seeing what you did I came up with an idea.
The only problem you have is that the pointer is not looking at north but at something else while it is still rotating with the character.
In your case you can easily add one simple value that adds up to the compass pointer's initial state.
The compass pointer will still rotate with your characters Y rotation but the compass pointer will point to a different direction than it normally would.
public float extraOffset; /* Edit this inside the editor on playmode until
your pointer points towards your north.
Then remember that value and set it while you are not in playmode.*/
public void ChangeNortDirection() {
northDirection.z = extraOffset + player.eulerAngles.y; // Just added the simple extraoffset variable and you are good to go.
northLayer.localEulerAngles = northDirection;
}
Also remember to explain your question as good as possible since it can be really hard for us to know what the exact problem is.
For example tell us if your game is going to be 2D or 3D and what the initial idea behind the subject of the problem is.
Hope this helps you out a bit.
Mennolp
I guess your question is related to the previous one : that's nice to see you're using Unity UI system now (but you could have given people over here a bit of feedback to their answers/comments) !
Regarding your current question, one of the best (and simplest) solution is to get your player's forward transform (blue axis in scene view) and to used it's X an Z values (Y doesn't matter here) to get the angle you want :
northLayer.localEulerAngles = new Vector3(0.0f, 0.0f, Mathf.Atan2(player.forward.x, player.forward.z) * Mathf.Rad2Deg);
This example is considering your northDirection vector is 0,0,1 and your northLayer is pointing north when its localEulerAngles.z is set to 0 (and of course that northLayer is a RectTransform component).
Hope this helps,