- Home /
Other
Applying Constant Force to Object A using Object B's Vectors.
Hey all,
I'm having a hard time with Constant Force and making it work in my project. Could you take a look at what I'm doing and tell me what I'm doing wrong or if I should look for another approach?
The basic gist is that I have a trigger box (wind zone) that I instantiate with a flick (swipe). I take the starting coordinate of my flick and assign it as a HitPointA using a Raycast. I then use the ending coordinate to get HitPointB, also using a Raycast.
Then, when my rectangular-shaped "wind zone" comes into play, it instantiates at HitPointA and then use Transform.LookAt to rotate the object to face HitPointB. This gives me a definite Vector3.Forward.
What I would like to do is set it so that when my primary game object, a flower petal, touches this wind zone, the flower will move along the wind zone in a direction that matches HitPointA to HitPointB.
Here the petal is falling, it's Z-coordinate is locked because this is a 2D game. It is about to hit the wind zone trigger box.
The problem is that "pointy" end of my wind zone (the clock hand, if you will) that points at HitPointB runs along the Z-axis. So my code (listed below - attached to my WindZone prefab) automatically tries to assign a ConstantForce value of Z: 10 to my petal.
All I want, is for my petal to travel within the wind zone in the direction that it is pointing. Thinking of a clock, if the hour hand is point at 3'oclock, I'd like it to travel from the center of the clock to towards the 3:00 position. If the hour hand is point at 9:00, then I'd like for it to travel towards the 9:00 position.
Any ideas on what I'm doing wrong or how I can fix this?
using UnityEngine;
using System.Collections;
public class WindStream : MonoBehaviour
{
GameObject petal;
Vector3 streamDir;
public float gravityTimer; // disable gravity within the stream
public bool startTimer; //trigger needed to start timer
void Start()
{
petal = GameObject.FindGameObjectWithTag("Petal");
streamDir = Vector3.Forward;
gravityTimer = 1.0f;
}
void Update()
{
if (startTimer == true)
{
gravityTimer -= Time.deltaTime;
if(gravityTimer <= 0)
{
ToggleGravity();
ResetTimer();
startTimer = false;
}
}
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.name == "Petal")
{
Debug.Log("Contact!");
startTimer = true;
petal.constantForce.relativeForce = streamDir * 10;
}
}
void ToggleGravity()
{
petal.rigidbody.useGravity = false;
}
void ResetTimer()
{
gravityTimer = 1.0f;
}
}
I've found that if I write the following -
transform.LookAt(swipeTeset.needlePoint$$anonymous$$agnet);
transform.Rotate(new Vector3(0, 90, 0);
That the X-axis is now pointing where I want my flower petal to go. The problem is that when the flower enters the trigger box, it's still moving according to its own coordinates, not the trigger box's. :(