Rotating object to another objects rotation [C#]
Hi! A little background: I have asked this question before and i thought it worked but now it doesn't work on some situation. I want to teleport my player to another place when he enters the portal and rotate him to thew portals exit. So when my exit portal is on the ground the player exits and is turning upwards(z = 270)! Code:
using UnityEngine;
using System.Collections;
public class StepThroughPortal : MonoBehaviour
{
public GameObject otherPortal;
public GameObject player;
Quaternion rotate;
void Start()
{
//rotate = Quaternion.LookRotation(otherPortal.transform.forward);
}
void Update()
{
}
public void Interact(Transform collidedObject)
{
Debug.Log("something hit the portal");
collidedObject.position = otherPortal.transform.position + otherPortal.transform.forward;
collidedObject.rotation = Quaternion.LookRotation(otherPortal.transform.forward);
TrackVelocity velocityScript = collidedObject.GetComponent<TrackVelocity>();
if (velocityScript != null)
collidedObject.GetComponent<Rigidbody>().velocity = Quaternion.FromToRotation(transform.forward, -otherPortal.transform.forward) * velocityScript.Velocity;
}
void OnCollisionEnter(Collision collidedObject)
{
Interact (collidedObject.transform);
}
}
Special request for help: @dhore who have helped me in the previous question greatly! Thanks in advance. Ethan
Sorry, I was away with friends over the weekend :3 Glad to see that you fixed it though!
Actually now i was debugging again and when i get out of the portal it doesn't change the player rotation to the portal direction.... If you still can please help me. $$anonymous$$y code is as it is written before. Thanks Ethan
1) Not sure what that part about the TrackVelocity script is, but it seems to be spinning it in circles or something? $$anonymous$$aybe remove those 3 lines?
2) Ins$$anonymous$$d of collidedObject.rotation = Quaternion.LookRotation(otherPortal.transform.forward);
you should just be doing collidedObject.rotation = otherPortal.transform.rotation;
Answer by he77789 · Nov 23, 2018 at 11:40 AM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TeleportHandler : MonoBehaviour {
public Transform otherPortal;
private TeleportHandler portal;
[HideInInspector]
public bool ready = true;
// Use this for initialization
void Start () {
portal = otherPortal.GetComponent<TeleportHandler>();
Debug.Log(transform+" to "+portal);
ready = true;
}
// Update is called once per frame
void Update () {
}
private void OnTriggerEnter(Collider other)
{
Debug.Log("Enter");
ready = false;
if (!portal.ready) return;
Debug.Log("Transfer");
Vector3 delta = new Vector3(transform.position.x - other.transform.position.x, transform.position.y - other.transform.position.y, transform.position.z - other.transform.position.z);
other.transform.position = new Vector3(otherPortal.position.x - delta.x, otherPortal.position.y - delta.y, otherPortal.position.z - delta.z);
other.transform.Rotate((Quaternion.Inverse(transform.rotation) * Quaternion.Inverse(otherPortal.rotation)).eulerAngles);
}
private void OnTriggerExit(Collider other)
{
Invoke("Reset", 0.1f);
}
private void Reset()
{
ready = true;
}
}
Sorry for the lengthy code, that was what I used. However, it does not rotate inertia correctly. See <a href="https://answers.unity.com/questions/1574323/how-to-rotate-a-gameobjects-inertiahow-to-rotate-g.html">My Question</a>
Your answer
Follow this Question
Related Questions
Rotation working within an if statement but not within a method??? 0 Answers
Rotating a forward vector 0 Answers
Rotating an Object (To Face Another Object) Only on X and Y Axis 3 Answers
Cancel out the steering rotation returned from a wheel colliders .GetWorldPose(out pos, out rot); 1 Answer
How to make a 2D sprite rotate towards a specific point while facing perpendicular to the camera ? 1 Answer