Hi I have been searching google for some time now with no avail. I was wondering if there was some function or easy way to make an object rotate so it is pointing along a certain vector (in this case, pointing the same way it is moving, but I would like to know generally how to do this with any Vector3) in Unity? I know it is possible to rotate an object using a Vector3 holding EulerAngles, but that is not what I'm looking for, since the object will not point in the direction of that Vector in 3D space.
I believe you should be able to use either Transform.LookAt or Quaternion.LookRotation to accomplish your goal.
Additionally, here is a link to a good Unity answers post where they use Quaternion.LookRotation to get a character to face the position it's moving based on its vertical and horizontal movement input.
transform.forward = velocity.normalized;
Hope that helps =)
Related
Me and my friend are very new to UE4, and we are are trying to develop a game where your main method of movement is your gun's recoil.
We are having a lot of trouble adding impulse relative to where the player shoots, since the AddImpulse function seems to work relative to the world (So the player will always go in one direction, no matter where he is looking at when he shoots).
If anyone has any idea on how to make the AddImpulse function add an impulse in the opposite way of the player's view it would be of great help!
I figured it out after some frustration and help from the UE4 subreddit. Use the getworldrotation function for the camera, then multiply that by how much impulse you want and feed it into the AddImpulse function.
Following the google-vr sample I manage to add a camera and controller to my scene.
The next thing I need is to get the distance between my controller to any pointed game object in the scene.
After searching for a while, I cannot find any tutorial nor information on how to get the distance.
So, is there any newest working tutorial on how to do this? (Many tutorial on the internet is outdated since google updates its API so frequently)
Or it is actually a simple task i.e. I can get the value from GvrPointerInputModule.Pointer / GvrLaserPointer / some other GVR class?
Thanks in advance~
You need to do raycasts from the controller and measure the difference between the hit location and the origin of the Ray cast. I think unity raycasts can return this distance built-in.
Just as I suspected, GvrLaserPointer is the answer.
If its CurrentRaycastResult.gameObject is not null, then the laser is intersecting with something. Then, we can get the intersection point from CurrentRaycastResult.worldPosition.
Using this point, we can easily calculate the distance.
Note: Just in case anyone failing with this method, like I did before. Check your ray casting group. Make sure that your Raycaster Event Mask in GVRPointerPhysicsRaycaster only include the desired layers. And if you have any canvas in screen space, check its Blocking Mask in Graphic Raycaster. It's Everything by default and your pointer may keep intersecting with the canvas, resulting in "weird" intersection point. This the cause of my problem, and to fix it, I select Nothing for Blocking Mask, and voila.
Im curious how you would find how you would find how much someones head rotated using only a camera in c#. I dont know where to start with this so thats why im bringing it up on here.
If I'm understanding this correctly, there would be no simple way of telling how much a head has rotated in unity only using a camera. The only method I could think of would be some OR (optical recognition). My best recommendation would be looking into the rigging of the character to be able to tell, as that has the direct rotational, scaling, and positional values in reference to the character.
Example: This characters bone #3 (which isn't the head, but doesn't directly matter) has been rotated 100 degrees along the X, and -90 degrees along the Z.
I am trying (for learning purposes) to make a Portal game. I have the basics working, I can place two portals, and walking within the collider of one makes me teleport to the other, however I can't seem to get the facing direction/rotation to work.
I want to face outwards from the new portal after the teleportation.
I have tried the following, with no success:
var angle = thisPortalCamera.transform.rotation.eulerAngles.y - otherPortalCamera.transform.rotation.eulerAngles.y;
playerChar.transform.Rotate(Vector3.up, angle);
My idea here was that only the y-axis rotation really matters, and I think I should rotate the player by the difference in axis between the two portals. This is probably really simple and easy, but I am pretty new to Unity. Any suggestions?
The easiest way would be to set your portal so that its forward is the orientation you want you player to have. Then you just go with:
player.transform.rotation = portal.transform.rotation;
player.transform.position = portal.transform.position;
The aim is to have the blue arrow of your portal to point in the right direction.
One of the easiest way is to use the function LookAt() and Rotate() of the transform. This function take one parameter that is the Position the object has to look.
transform.LookAt(portal.position);
tranform.Rotate(new Vector3(0,180,0);
This will make your character face the opposite way of your portal.
Hi am developing a simple Space Shooter styled 2D game and I am stuck at the point where the Object should restrict itself moving beyond the left and right edges of the screen.
I implemented #Waz solution in one of the answers in Unity Answers and it works great if the object is not a rigidbody. However if it is applied to a rigidbody, the object starts to flicker. Below is the code that I used from #Waz
float speed = 0.1f;
Vector3 viewPos = Camera.main.WorldToViewportPoint(transform.position);
viewPos.x = Mathf.Clamp01(viewPos.x);
viewPos.y = Mathf.Clamp01(viewPos.y);
transform.position = Camera.main.ViewportToWorldPoint(viewPos);
Here is the link where #Waz mentioned his piece of code:
http://answers.unity3d.com/questions/148790/detecting-the-edge-of-the-screen.html
Here is a link that says to use an alternative solution for rigidbody but this code does not work for me:
http://answers.unity3d.com/questions/62189/detect-edge-of-screen.html
I am not sure how to modify the above code so that the object I touch and move does not flicker. Any help would be great.
You are translating from arbitrary float coordinates to the range [0,1] and back again. Likely the issue you are running into is due to floating-point inaccuracies when your world-position is far away from 0.
There are multiple ways of solving this:
In your above script, only perform the transform if they are actually touching the edge of the screen.
Handle the OnBecameVisible() and OnBecameInvisible() messages. Don't let the player move off screen if it would cause them to "go invisible".
Use the IsVisibleFrom() callback from this wiki article. Some people prefer this because they claim that "OnBecameVisible()/OnBecameInvisible() are broken."
I don't know how/why they believe they're broken.
Have you tried using Screen.width and Screen.height in detecting the edge of the screen? Maybe it can help in the prevention of the flickering.