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 ezraza · Jan 29, 2014 at 05:59 AM · c#physicsrigidbodytriggers

Launching a player in a certain direction - C#

I am trying to create a boost pad that when the player enters they will be launched into the air. I have created the basic effect of launching the player into the air, but I also want to have it so when the players is launched, they are also launched forward in the direction of another platform without the player having to guide themselves to the other platform.

Here is my code right now.

     using UnityEngine;
     using System.Collections;
     
     public class Boost_pad : MonoBehaviour {
     
         public float velocity = 10.0f;
         public float force = 20.0f;
         
         // Use this for initialization
         void Start () {
             
         }
         
         // Update is called once per frame
         void Update () {
             
         }
         
         void OnTriggerEnter(Collider c)
         {
             if (c.tag == "Player")
             {
                 c.rigidbody.velocity = Vector3.up * velocity;
             }
         }
         
         void OnTriggerExit(Collider c)
         {
             if (c.tag == "Player")
             {
                 c.rigidbody.AddForce(Vector3.forward * force, ForceMode.Acceleration);
             }
         }
     }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by supernat · Jan 29, 2014 at 07:02 AM

Is your OnTriggerExit() event happening?

In either case, you're using Vector3.forward which is static and unchanging and always along the +Z axis. So if you're doing a side scroller moving along the +X axis, or some variation thereof, this won't work. You just need to change Vector3.forward to Vector3.right or -Vector3.right if you're moving along the -X axis. If it's in 3D, you need to compute the normalized direction cosine from the player to the next platform. In other words:

 Vector3 dir = (platformposition - playerposition).normalized();

And use that instead of Vector3.forward.

You can also zero out the Y component of the dir vector to make the player move along the bearing toward the platform without them moving up/down:

 Vector3 dir = platformposition - playerposition;
 dir.Y = 0;
 dir.Normalize();
Comment
Add comment · Show 3 · 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 ezraza · Jan 29, 2014 at 07:45 AM 0
Share

I tried adding this to my code in the OnTriggerExit function and still only received the jumping effect. It did not send the player in the direction of the platform.

avatar image supernat · Jan 29, 2014 at 09:33 PM 0
Share

Which code? Are you still using acceleration ins$$anonymous$$d of velocity? Did you try increasing the amount of force you apply? Does OnTriggerExit actually get called when you think it will?

avatar image ezraza · Jan 30, 2014 at 12:52 AM 0
Share

We have changed the code entirely. We now can launch a rigidbody object in an arc from one position to another. The only problem is that we can not affect the actual player rigidbody to move forward, but only upwards.

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make Rigidbody.AddForce less delayed in Unity3D? 0 Answers

How to make a dynamic bow shot and the arrow to curve and turn correctly when shot? 0 Answers

Rigidody.velocity = Direction * speed; How to get direction? 1 Answer

Push Object in Opposite Direction of Collision 3 Answers

Can I Prevent Rigidbody Drag from Affecting the Z-Axis? 2 Answers


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