Okay, so I am trying to code an object in 3D to move however I want it to move in that 3D space.
For example, make it move along a certain path and also for a certain amount of time.
This is my attempt at trying to do this:
using UnityEngine;
using System.Collections;
public class Motion : MonoBehaviour
{
void Start()
{
StartCoroutine(Process());
}
private IEnumerator Process()
{
yield return StartCoroutine(GoForward(0, 2));
yield return StartCoroutine(GoForward(1500, 7));
yield return StartCoroutine(GoForward(500, 3));
StartCoroutine(GoRight(100, 4));
yield return StartCoroutine(GoForward(500, 4));
yield return StartCoroutine(GoForward(1000, 5));
StartCoroutine(GoLeft(100, 3));
yield return StartCoroutine(GoForward(1000, 3));
yield return StartCoroutine(GoForward(1500, 13));
StartCoroutine(GoLeft(100, 2));
yield return StartCoroutine(GoForward(1000, 2));
yield return StartCoroutine(GoForward(1500, 6));
StartCoroutine(GoLeft(100, 2));
yield return StartCoroutine(GoForward(100, 2));
StartCoroutine(GoRight(100, 2));
yield return StartCoroutine(GoForward(100, 2));
StartCoroutine(GoLeft(50, 2));
yield return StartCoroutine(GoForward(100, 2));
yield return StartCoroutine(GoForward(100, 4));
StartCoroutine(GoRight(100, 2));
yield return StartCoroutine(GoForward(100, 2));
StartCoroutine(GoLeft(100, 2));
yield return StartCoroutine(GoForward(100, 2));
StartCoroutine(GoRight(100, 2));
yield return StartCoroutine(GoForward(100, 2));
yield return StartCoroutine(GoForward(150, 5));
yield return StartCoroutine(GoForward(50, 2));
}
IEnumerator GoForward(int strength, float time)
{
GetComponent<Rigidbody>().AddForce(Vector3.forward * strength); //Adding Force ; Starts
yield return new WaitForSeconds(time);
GetComponent<Rigidbody>().AddForce(Vector3.back * strength); //Removes Force ; Stops
}
IEnumerator GoBack(int strength, float time)
{
GetComponent<Rigidbody>().AddForce(Vector3.back * strength); //Adding Force ; Starts
yield return new WaitForSeconds(time);
GetComponent<Rigidbody>().AddForce(Vector3.forward * strength); //Removes Force ; Stops
}
IEnumerator GoUp(int strength, float time)
{
GetComponent<Rigidbody>().AddForce(Vector3.up * strength); //Adding Force ; Starts
yield return new WaitForSeconds(time);
GetComponent<Rigidbody>().AddForce(Vector3.down * strength); //Removes Force ; Stops
}
IEnumerator GoDown(int strength, float time)
{
GetComponent<Rigidbody>().AddForce(Vector3.down * strength); //Adding Force ; Starts
yield return new WaitForSeconds(time);
GetComponent<Rigidbody>().AddForce(Vector3.up * strength); //Removes Force ; Stops
}
IEnumerator GoLeft(int strength, float time)
{
GetComponent<Rigidbody>().AddForce(Vector3.left * strength); //Adding Force ; Starts
yield return new WaitForSeconds(time);
GetComponent<Rigidbody>().AddForce(Vector3.right * strength); //Removes Force ; Stops
}
IEnumerator GoRight(int strength, float time)
{
GetComponent<Rigidbody>().AddForce(Vector3.right * strength); //Adding Force ; Starts
yield return new WaitForSeconds(time);
GetComponent<Rigidbody>().AddForce(Vector3.left * strength); //Removes Force ; Stops
}
}
Essentially, this kinda works. This lets me move my object in a certain direction for x amount of time (in seconds) and an arbitrary strength. However, my issue with my implementation is that the force gets applied once each time it is called and looks more like a jolt, and not very smooth. So what I’d like to make it look more like is like a continuous force that is applied for x amount of time.
So right now, it is more like GoForward looks like it impulses Forward only once but what I want is that it continuously impulses Forward for the allotted time. Does anyone think they can figure out how I could implement this?