I'm new to unity and I'm currently working on a portal-like game.
I did the whole teleportation script and it works, but the problem comes that I didn't implement the player camera correction and actually I don't have any ideas how to do it. The concept is that when you're jumping through a portal, the player (or player camera) rotation should be changed to the portal/portal camera rotation from you've come so the final effect is more 'realistic'.
I've tried some lines in teleportation script like player.transform.rotation = portal.transform.rotation but in the end it didn't work and now I end up with nothing, deleting previous scripts and trying to write it all over and over again.
I'll be glad if someone could guide me how to start coding it. Should I do it in onTriggerEnter (when you're jump through portal), or in onTriggerExit? Should the script be attached to a player or to a portals? Should I gather rotation only from camera or from the whole gameobject (portal/player)? I'm posting also couple of screens (with a video how it currently works, and also an entire teleportation script. If I missed something just ask me and I'll post it here.
https://imgur.com/a/pbqYnLD - screens with portals inspector
https://streamable.com/b14hk - video how it works
teleportation script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Teleportation : MonoBehaviour {
[SerializeField] private GameObject otherPortal;
[SerializeField] private GameObject player;
void OnTriggerEnter(Collider col) {
if(col.tag == "Player") {
col.transform.position = new Vector3(otherPortal.transform.position.x+1, otherPortal.transform.position.y+1, otherPortal.transform.position.z+1);
Debug.Log("wszedłem w portal");
}
}
void Update() {
}
}
some informations how it is coded right now:
portals are currently in game behind 'the box', i didnt instantiate them anywhere; just changing position on lpm (blue portal) and ppm (orange portal)
portals are sticking to the walls, just like in the original game
portals have a camera attached to it and right now the cameras are static. (offtop: i have a script to move them exactly when player is moving and it quite works but also have some problems, like camera can get too far away from portal and start rendering only that green outer side of the box, and i also dont know how to fix it, so currently i didnt use this script)
the player movement im using is that from unity standard assets (if it somehow matters)
the player have a rigidbody but the portals dont; not sure if i should attach this component to them
teleportation script is attached to the both of portals - the 'otherPortal' variable is moved from inspector, like in orange portal the 'otherPortal' variable is blue portal and the other way
What you did is correct (setting the player rotation to the portal.
You can do it in onTriggerEnter after setting the position, then it should look like
player.transform.rotation = otherPortal.transform.rotation
If you do that, the player will have the same rotation. You already have something that make the camera follow the player, so it is likely that you don't need to set the camera rotation. I don't know how you did your camera follow, so I can't be sure, though. If the camera has not the proper orientation, doing Camera.main.transform.rotation = otherPortal.transform.rotation will do it.
The remaining thing that might be wring, could be that your player (and camera) is not facing the right axis. On your video, I can see that the portal faces the x-axis (the red axis in Unity editor). Check that when going forward, your player has the red axis looking forward.
It is likely that your player has the z-axis (blue) facing forward, which is (by convention) more correct and fits the names Unity uses (z-axis is also called forward-axis)
I would recomand to create the portal object (and all other objects, including the player) so that the forward-axis is the blue one. It might require editing the objects. Anycase, check that the player forward axis is that same as the portal, otherwise setting the rotation won't work
Related
Please is it possible to make a Player with rigidbody or character controller run on wall just like The Flash. Please guide me on how to implement this.Here's a link of what I'm talking about. Thank you in advance.
Rotate the player to be vertical to the building, adjust the gravity parameter on your rigid body (make it lower so the added force won't be canceled by it) and just add the force (or set the velocity, whatever makes you happy)
I'm going to assume you already know how to move the player around on the ground. All you need to do is create some form of detection for when they want to begin running on the wall (maybe the player presses a button, or walks up to the wall), at which point you will want to do 2 things: rotate the player to have their feet on the wall, and change gravity to be pulling the player into the wall.
If we assume the player is facing the wall when they go up on it, you can just rotate 90 degrees backwards. Changing gravity should be easy too. Once the player is fully rotated, do something like this
Vector3 newGravityVector = tranform.down.normalized
Physics.gravity = transform.down.normalized * 9.8 //9.8 is the default gravitaty value, feel free to use a different multiplier if you wish.
(this script should be attached to your player)
Hey i am making a unity game called Cube-Runner. There is a cube and obstacles and you have to go between them. Not going into the game a lot but the problem is how to follow the player. I can't parent it as if i do that while the cube falls the camera will also move with the rotating cube and will make the player(At least myself) dizzy.
Please give me a script
There needs to be a offset Vector3 which i can change from the inspector.
The offset Vector3 may work.
It should be written in C#.
NOTE: I AM NEW TO C# AND UNITY DO NOT JUDGE BY QUESTION
if you dont want to make the player the parent of the camera, then you can do this :
Create a C# script called CameraMovement and attach it to the camera
add this to CameraMovement
using UnityEngine;
class CameraMovement : MonoBehavior
{
public Transform player;
public Vector3 offset;
void Update()
{
//get the players position and add it with offset, then store it to transform.position aka the cameras position
transform.position = player.position + offset;
}
}
click on the camera and look at the inspector, you should see that there is a script called CameraMovement and 2 fields : player and offset. assign player with your player (drag and drop) and offset with the relative position between your camera and the player (where the camera is with the player being the center).
and you're done, play the game and see the results
You could try using the cinemachine tool, it will make you camera follow smoothly to the player. You could check any tutorial on youtube but I recommend you to check the one "Brackeys" did. No code needed for cinemachine
For an Oculus Quest game i'm working on, I need to be able to grab an object and not rotate it in any way. I should be able to move it in the x, y and z axes though. I'm doing this in a climbing game and the object is quite big. My player is locked on (0,0,0) and you climb by grabbing the terrain and moving it, giving the illusion that you are climbing.
I am using Unity's Oculus integration asset and I have the OVR Grabbable script on the object I want to be able to grab.
How do I make sure that the object I'm grabbing, doesn't rotate at all?
I've tried using a rigidbody and locking the rotation of the wall I want to climb like that, but that doesn't work. Once I grab it, I can still rotate the object.
I have also tried locking the rotation of the hand rigidbody, but that setting seemed to be ignored, because I could still rotate the hands.
I've also tried adding a bit of code in the script, which would reset the objects rotation in the fixed update. I put this code in the OVR Grabbable script.
void FixedUpdate()
{
transform.rotation = Quaternion.identity;
}
Using this code didn't keep the wall from rotating, but it did snap back to rotation (0,0,0) every frame. THis caused the wall to function as if it would still rotate, but it looked like it was switching between (0,0,0) and the rotation it would be at in every frame. This is of course also not the desired result.
I am not using VRTK, because that does not work with the type of climbing I'm trying to achieve.
I would like to be able to grab an object, move it in the x, y and z axes, while it doesn't rotate at all. Currently, I can still rotate the object. How would I fix this issue and completely lock the rotation whenever I grab the object?
If the object has become a child of the hand and you still want it to move but not rotate. You could add a simple script which scores its default rotation and applies it in LateUpdate.
This is designed for non-physics objects so be sure to remove your test where you added the rigidbody to the wall.
Something simple like this would do the job.
Quaternion defaultRotation;
void Awake()
{
defaultRotation = transform.rotation;
}
void LateUpdate()
{
transform.rotation = defaultRotation;
}
So I have a player. It has hands attached made from two rectangles.(I attached .GIF)
So I need it to move how the player moves right stick on joystick. I made some controls, but I think they are poor, and inaccurate. I believe, it's possible to make it better. Hope I will find some help here. What I made up is an object (invisible, visible only for describing problem, debuging purposes) that moves on axis of joystick. Then there are player, he has hands. On the lower hand lower there is script attached. Before I had made spring, but I didn't liked result. Script:
public GameObject Target;
void Update () {
if (Input.GetKey (KeyCode.J)) {
transform.position = Vector3.MoveTowards(transform.position, Target.transform.position, 1);
}
So the question is, how to upgrade this system hands control system? I would like it to be more physical, because I'm making a volleyball game.
I am working on a Unity 3D game for Oculus and I have problems with making my objects to apply physics on a player. So getting rid of a CharacterController and using something like a rag doll is not an option.
I am using OVRPlayerController, that has a Rigidbody with mass 1 and a box collider on it. My gameObject has a Rigidbody of mass 100, and a box collider. But when the object hits the player it just goes through it, whereas I want it to push the player in x direction.
I tried using onColliderHit but it doesn't even recognize the collision between the player and the object, so I checked box collider on the object to be a trigger and I use OnTriggerEnter() to recognize the collision.
I tried to translate the player's position on collision, but player gets positioned to weird places out of my map for some reason. Here is what I use:
info.transform.Translate(new Vector3( -0.5f, 0.0f, 0.0f));
info.transform.rotation = Quaternion.identity;
I also tried to manually set the x position of the player but this doesn't work, and I know I am not supposed to do it.
I searched for answers for a long long time, so please don't answer to this with something like "oh, have you tried googling it, there are a lot of similar questions" etc.
It should be detecting the collision just fine, as long as one of the box colliders has a non-kinematic (Is Kinematic = false) rigidbody attached.
Make sure Is Trigger is false, and you specified a material for your collider.
Also try messing with the other properties on the rigidbody, such as Collision Detection and Mass. The unity docs indicate that your mass should be no more or less than 100 times that of other rigidbodies.