- Home /
How to make a Vector with the same angle as a certain object?
Hi Im making a Portal style game in Unity but in 2D. And I got stuck while making the portal system (Im a Unity noob). I want my character to be shoot out of the portal that he exited in the direction that the portal is facing (with Rigidbody2D.AddForce).
I also want the AddForce vector "power" to be dependent of the velocity that the character has before entering the first portal. I have tried everything I could have come up with and I cant seem to figure it out (without making a ton of IF statements... sigh). Any help Please?! :)
Im kind of bad at explaining so I made 2 pictures in Paint to demonstrate (excuse my shitty Paint skills ;) ):
http://i.imgur.com/5QjGGVH.png
http://i.imgur.com/V6ftd2o.png
It would be probably be one of the following vectors: transform.forward, transform.right, transform.up, where transform is the transform of the portal object.
@_Gkxd Wait, why would I need transform here? transform.forward, transform.right and transform.up Point to the global axises right? transform.up for example points to the global Y-axis and not what is up from the portals point of view, right? Or am I wroing, i have no idea
transform.forward is the forward in the object coordinates. Vector3.forward is the world z axis and is constant.
Hmm, I tried a solution using IF statements but on OnTriggerEnter It detects only the first if statment ( if(otherportal.transform.rotation.z == 0) ) and not the second one ( if(otherportal.transform.rotation.z == 90) ). I dont understand why.
Heres my code: using UnityEngine; using System.Collections;
public class PortalScript : $$anonymous$$onoBehaviour {
public GameObject otherportal;
public GameObject player;
private Collider2D otherportalCollider;
public Rigidbody2D otherRigidBody;
void Awake(){
otherportalCollider = otherportal.GetComponent<BoxCollider2D>();
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void FixedUpdate(){
}
void OnTriggerEnter2D(Collider2D other) {
if(other.tag == "Player"){
otherRigidBody = other.GetComponent<Rigidbody2D>();
other.GetComponent<BoxCollider2D>().enabled = false;
other.GetComponent<CircleCollider2D>().enabled = false;
if(otherportal.transform.rotation.z == 0){
other.transform.position = new Vector2(otherportal.transform.position.x + 1,otherportal.transform.position.y + 1);
otherRigidBody.AddForce(otherportal.transform.up * otherRigidBody.velocity.magnitude, Force$$anonymous$$ode2D.Impulse);
}
if(otherportal.transform.rotation.z == 90){
other.transform.position = new Vector2(otherportal.transform.position.x,otherportal.transform.position.y);
otherRigidBody.AddForce(otherportal.transform.right * otherRigidBody.velocity.magnitude, Force$$anonymous$$ode2D.Impulse);
}
}
}
void OnTriggerExit2D(Collider2D other){
if(other.tag == "Player"){
other.GetComponent<BoxCollider2D>().enabled = true;
other.GetComponent<CircleCollider2D>().enabled = true;
}
}
}
rotation is of type Quaternion, the z component moves between 0 and 1 and not in a way you can visualise, you should see my answer on how to do it with Quaternions.
Answer by Scribe · Aug 29, 2015 at 05:06 PM
You could 'undo' the effect of the entered portals rotation, by multiplying by the inverse of its rotation. The at the other end, redo the rotation by multiplying by the new portals rotation:
The following is pseudo code
Vector3 velocity;
Quaternion rotation;
Vector3 position;
void OnEnterPortal(Collider2D collider){
if(collider is not a portal) return; //make sure we are hitting a portal
GameObject thisPortal = collider.gameObject;
GameObject otherPortal; //get your other portal somehow
//setup mapping rotations
Quaternion undoRot = Quaternion.Inverse(thisPortal.transform.rotation);
Quaternion redoRot = otherPortal.transform.rotation;
velocity = undoRot * ourCharacter.velocity;
rotation = ourCharacter.rotation * undoRot;
position = undoRot * (ourCharacter.position - thisPortal.transform.position);
//Set new variables
ourCharacter.position = otherPortal.transform.position + (redoRot * position);
ourCharacter.rotation = rotation * redoRot;
ourCharacter.velocity = redoRot * velocity;
}
Hope that helps!
Your answer
Follow this Question
Related Questions
rigidbody problem 1 Answer
How to fix/clamp rigidbody dir vector, despite Force dir 1 Answer
Direction of rotation and ApplyForce 1 Answer