Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Oninji · Oct 21, 2010 at 07:48 PM · rotationvelocitydirectionforce

Teleport and change direction of current velocity.

Hellow,

From the title it isn't really clear, so here exactly what I want to do.

Firstly, I'm teleporting an object to another location. (Done through a transform.position)

Secondly, (the part where I fail)

Retain the Force/Velocity of the object, but change it's direction.

As a whole, The object enter the First gate and should jump out of the Second gate relative to where the Second gate is facing.

I've thought of reading my velocity, and give it back to the ball at his exit relative to the space of the exit. But It seems I can only adjust space to Self or World...

Any idea?

Edit

Here a Link to a simple Image depicting what it should do.

http://img6.imageshack.us/img6/7009/schemgate.jpg

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by VS48 · Oct 21, 2010 at 07:58 PM

Your object's velocity has two logical parts -- the direction, and the magnitude (how fast you're moving). We don't want to change the magnitude, but we will change the direction.

You can use the transform of your second gate to get the direction. I don't know exactly how your gate model looks, but I will assume that its exit is facing in the direction of the positive Z axis. (You can verify this by clicking on the object in the Scene with Local coordinates selected)

So, you would need to do at least two things: change the object's direction to be the same as the second gate, and change the object's position to be same as the second gate. Something like this might work:

MyObject.transform.position = SecondGate.transform.position;
MyObject.rigidbody.velocity = SecondGate.transform.forward * MyObject.rigidbody.velocity.magnitude; // SecondGate.transform.forward is a vector pointing down the positive Z, in local space

Edit:

Saw your answer. The velocity might be getting zeroed because you are not using triggers for your gates or the position change zeroes it in the same tick.

Here's code with trigger gates instead of solid gates, which doesn't require re-instantiating the object and raping the GC:

function OnTriggerEnter(collider : Collider)
{
    if (collider.attachedRigidbody != null && collider.attachedRigidbody.CompareTag ("Ball")) 
    { 
        collider.attachedRigidbody.position = destination.transform.position;
        collider.transform.forward = destination.transform.right;
        collider.attachedRigidbody.AddForce(destination.transform.right * collider.attachedRigidbody.velocity.magnitude * powerYield, ForceMode.VelocityChange);
            // Or collider.attachedRigidbody.velocity = destination.transform.right * collider.attachedRigidbody.velocity.magnitude * powerYield
    } 
}
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Oninji · Oct 21, 2010 at 08:14 PM 0
Share

For now they are cube, just developping the script. But tried your way, it's the same as a simple transform.position.

avatar image VS48 · Oct 21, 2010 at 08:30 PM 0
Share

Yes, you do need to change the position. Either transform.position or rigidbody.position. You will also need to change the direction of the velocity vector and the orientation of your object.

So you could also add: $$anonymous$$yObject.transform.forward = SecondGate.transform.forward; to update the orientation on exit.

avatar image Oninji · Oct 21, 2010 at 11:10 PM 0
Share

Still does nothing more than change it's position.

avatar image VS48 · Oct 22, 2010 at 04:51 AM 0
Share

See edit. Glad you got it to work!

avatar image
0

Answer by Oninji · Oct 21, 2010 at 11:37 PM

Found a way to "Do it"

var destination : Transform; 

var powerYield = 2000; //Most realist is 2000.

private var power : float = 0; private var ImpactVelocity : float = 0;

function OnCollisionEnter(collision : Collision) { if (collision.gameObject.CompareTag ("Ball")) { ImpactVelocity = collision.relativeVelocity.magnitude;

power = ImpactVelocity * powerYield;

     Destroy (collision.gameObject);

         var ball = Instantiate(collision.gameObject, destination.transform.position, destination.transform.rotation);
         ball.rigidbody.AddForce(destination.transform.right * power);

}

}

I detect the velocity and then instantiate with power by multiplying velocity value.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to get the direction of the velocity of an object (c#) 1 Answer

Gun not firing in certain direction 1 Answer

Calculating the difference between forward direction and velocity direction to set the body roll of my vehicle. 1 Answer

how to find rotation from direction of velocity 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges