How to transform a GameObject to an Empty GameObject's position and rotation
Um, I suppose I don't know vector3 very well, but I was working with a portal script i found on the internet and tried to change it to do what I want, which is change my position and rotation to the portal's "spawn" gameobject which is used through public string to be the teleportTo var. The code that I had found on the internet worked for re-positioning, but didn't include the rotation part, and if the portals are placed a certain way, the player flies threw them continuously until the player's momentum has stopped. My change to the code does work, instantiating/deleting a string-ed player, which seems rather inefficient, it lags insanely, and does not let my grab/drop script work, since the player becomes a clone. I'd rather use a transform function, which simply moves & rotates the player. I attempted to a few different ways to get the transform to work, but none ever worked correctly. This code is the imitation of the function in which I want.
var teleportTo : Transform;
var player : GameObject;
function OnTriggerEnter (col : Collider)
{
if(col.gameObject == player)
{
Instantiate(player, teleportTo.position, teleportTo.rotation);
Destroy (player);
}
}
The error I got was "The object of type 'CapsuleCollider' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object." which I understand why that is happening, just thought i'd note it.
What I was wondering was if there was a way which I have not learned (I have checked out the tutorials, some things I just need a better understanding of, as the tutorials understandably dont show every way to use the bit of code) to make the player onEnter of the portal's trigger to go to teleportTo's position and rotation. I would also rather the player lose all momentum than reflect it, it will not be used like valve's portals. Thanks in advance, and I know, I need to study up on coding more, I'm just busy all the time, so I'd rather have the answer and description of why it works better than try to decipher other's codes which don't apply to my issue directly, and are difficult to apply to my issue.
Answer by SomeGuy22 · Dec 20, 2015 at 08:35 PM
Seems like a lot of unnecessary work for a simple transform change... Instantiating and deleting are heavy tolls on performance. Instead, replace that code with this:
player.transform.position = teleportTo.position;
player.transform.rotation = teleportTo.rotation;
"Momentum" changes should happen within your motor script. Unity's Character Motor has a SetVelocity() function which allows you manually enter a velocity. A velocity of Vector3.zero will stop motion.
@SomeGuy22 It is a bit of work, yes. Thank you for your input, especially the bit about velocity, but the rotation part of the code you gave does not work (I had tried before and it had the error that "player" on the second line wasn't a reference to anything, but now it doesn't have that error). It seems as though it would work when I read it, but it doesn't effect the player's rotation in game play mode.
@SomeGuy22 Eh, hmm, I suppose I spoke to soon, I turned smoothness on my character's camera rotation and apparently my character turns really fast to match the cardinal direction it had before teleporting after assu$$anonymous$$g teleportTo's rotation. I'll mess with some scripting later to try to correct this.
Your answer
Follow this Question
Related Questions
How can I make an object transform to given position and rotation 2 Answers
Enemy not facing player when enter rotation orbit 0 Answers
Car Controls without physics. 1 Answer
How do I change the position of my player model after I rotate it so it's not teleporting? 0 Answers
Quaternion.RotateTowards for camera positions is buggy 1 Answer