- Home /
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);
}
}
}
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();
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.
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?
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
Follow this Question
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