- Home /
Vector3 Parameters and AddForce Not Working Out
So, I'm pretty new to unity and C# so I understand that I'll have mistakes, but I can usually work around them. I have an issue with my game where I want a "meteor" to reset itself after it hits the ground. It kind of is, but not really. The force seems to be continuous and doesn't reset itself, and for some reason my Vector3 is broken (at least the Random.Range part is). Here is the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class Meteors : MonoBehaviour
{
public float FallRate;
public float xMin, xMax, zMin, zMax, yMin, yMax;
// Start is called before the first frame update
void Start()
{
transform.position = transform.position + new Vector3(Random.Range(xMin, xMax), Random.Range(yMin, yMax), Random.Range(zMin, zMax));
GetComponent<Rigidbody>().AddForce(new Vector3(0.0f, FallRate * Time.deltaTime, 0.0f), ForceMode.Impulse);
Debug.Log("I'm adding force!");
}
void OnTriggerEnter(Collider other)
{
GetComponent<Rigidbody>().AddForce(-Physics.gravity * Time.deltaTime, ForceMode.Acceleration);
transform.position = transform.position + new Vector3(Random.Range(xMin, xMax), Random.Range(yMin, yMax), Random.Range(zMin, zMax));
}
}
Now, I'm just not sure what I'm doing wrong and why my Vector3 and added force isn't resetting itself. If you think my question is vague or have any questions, I'll get back to you ASAP. Thanks! I'm using gravity btw.
Answer by DemocracyDealer · Jul 18, 2020 at 01:43 PM
I was able to fix it by disabling gravity and creating and artificial gravity equation. For the Vector3 I just switched something up and it worked. Here is the final code.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Meteors : MonoBehaviour { public float FallRate; public float xMin, xMax, zMin, zMax, yMin, yMax;
// Start is called before the first frame update
void Start()
{
float gravity = FallRate * -1000 * Time.deltaTime;
Vector3 startPosition = new Vector3(Random.Range(xMin, xMax), Random.Range(yMin, yMax), Random.Range(zMin, zMax));
GetComponent<Rigidbody>().AddForce(new Vector3(0.0f, gravity, 0.0f), ForceMode.VelocityChange);
transform.position = startPosition;
Debug.Log("I'm adding force!");
}
void OnTriggerEnter(Collider other)
{
Vector3 startPosition = new Vector3(Random.Range(xMin, xMax), Random.Range(yMin, yMax), Random.Range(zMin, zMax));
GetComponent<Rigidbody>().AddForce(Vector3.zero);
transform.position = startPosition;
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class $$anonymous$$eteors : $$anonymous$$onoBehaviour { public float FallRate; public float x$$anonymous$$in, x$$anonymous$$ax, z$$anonymous$$in, z$$anonymous$$ax, y$$anonymous$$in, y$$anonymous$$ax; public float IncreaseRate;
// Start is called before the first frame update
void Start()
{
// Artificial gravity equation
float gravity = FallRate * -1000 * Time.deltaTime;
Vector3 startPosition = new Vector3(Random.Range(x$$anonymous$$in, x$$anonymous$$ax), Random.Range(y$$anonymous$$in, y$$anonymous$$ax), Random.Range(z$$anonymous$$in, z$$anonymous$$ax));
GetComponent<Rigidbody>().AddForce(new Vector3(0.0f, gravity, 0.0f), Force$$anonymous$$ode.VelocityChange);
transform.position = startPosition;
Debug.Log("I'm adding force!");
}
void OnTriggerEnter(Collider other)
{
float gravity = FallRate * -1000 * Time.deltaTime;
Vector3 startPosition = new Vector3(Random.Range(x$$anonymous$$in, x$$anonymous$$ax), Random.Range(y$$anonymous$$in, y$$anonymous$$ax), Random.Range(z$$anonymous$$in, z$$anonymous$$ax));
transform.position = startPosition;
GetComponent<Rigidbody>().AddForce(new Vector3(0.0f, gravity * IncreaseRate, 0.0f), Force$$anonymous$$ode.VelocityChange);
}
}