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 DemocracyDealer · Jul 18, 2020 at 01:06 PM · rigidbodyvector3gravityaddforcerandom.range

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.

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
0
Best Answer

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;
 }

}

Comment
Add comment · Show 1 · 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 DemocracyDealer · Jul 18, 2020 at 02:14 PM 0
Share

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);
 }

}

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

209 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 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 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 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 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 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 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 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 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 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 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

AddForce to a randomly selected GameObject with a rigidbody 0 Answers

Very simple water bouyancy script not working? 1 Answer

Vector3 is always subject to serious stutter in Translation 0 Answers

Change rigidbody's jumping speed 2 Answers

Addforce.forward on a sphere object 1 Answer


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