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.
Related
I was wondering if you could tell me if this is possible. I've spent a couple hours to no avail trying to find a way to make a object with box collider swing inwards and outwards like a door. I used hinge joint, but it will only swing the gameobject inwards or outwards around the joint. I am trying to achieve a 'saloon door' effect that allows free rotation around the y axis. I have tried fiddling with the anchor position and axis values but no luck. I was surprised not to find any similar problems online, perhaps hinge joint is not meant for this purpose? Any clarification is much appreciated.
I'm not really sure how I fixed it, but I got the door to swing freely. One possible reason is surrounding colliders or terrain prevented it from swinging freely. sigh
I have this ball that i want to move by rolling like a certain alien, with it spinning on its transform.up to change its direction and rolling on its transform.right to move forward and backward. But as it rolls, its up and right transform vector gets messed up.
In the end, i was unable to find a way to allow it to roll without messing up my directions vector. I have no clue as to how to do this. So yeah, please help. ORZ. By the way, i'm not creating a game using the Ben10 franchise. The ball should act like a wheel... a really round and spherical one.
Note:
I want to use physics in this game. Since, i want it to be able to launch itself off a ramp or something. So, only addforce or addtorque or other relevant functions.
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 =)
I want to move a cube by making it roll on it's edges. The cube will be standing on a x-z grid and each movement it takes will make it stand on a different square of the grid.
The player will controll the movement and will only be able to make the cube roll on one direction at a time (left, right, forward or back), but the cube must always stand exactly on top of one of the grid's squares.
I don't think applying a force to the cube will help cause it could move it a little bit too much or too little. I want to achieve something like this: https://www.youtube.com/watch?v=yaAIUYuNi84 but only on the x-z plane. Notice how on each corner the cube can stop and change direction with ease, because it never moves too much or too little.
Any ideas on how to approach this?
If you are new to the Unity it will be useless to trowing you a bunch of codes so i am telling you the way of doing it so you can implement your own codes.
You can create 4 empty game object which will always follow the cube on the floor and when you want to roll the cube you will rotate the cube around the empty objects.
You can find the codes for following the cube and rotating the cube on youtube, and for the starters searching is allways good.
So i hope you can manage it out, if you cant please write me again where did you stuck and i will be happy to answer you :)
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.