Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 javanullpointer · Oct 09, 2021 at 06:59 PM · movementinstantiateforcecircular

circular movement with force not working

I have a stardart sphere game object which is prefab with rigidbody and target script. in target script Im giving circular movement to this sprere object.

And I have spawn script which accepts 3 point and 3 object which Instantiates theese 3 object in 3 point.

My circular movement is working, but when i try to add force through the camera it doesnt work.

How Can I achive both circular movement and add force through the camera?

My target and spawn scripts are below.

target

public class Target : MonoBehaviour { public float health = 50f;

 public float speed;
 public float width;
 public float height;

 float timeCounter = 0f;
 Vector3 startPoint;

 int direction;

 void Start()
 {
     int number = Random.Range(1, 255);
     direction = number % 2 == 0 ? 1 : -1;
     startPoint = new Vector3(transform.position.x, transform.position.y, transform.position.z);
 }

 // Update is called once per frame
 void Update()
 {

     timeCounter += Time.deltaTime * speed;

     float x = direction *  Mathf.Cos(timeCounter) * width + startPoint.x;
     float y = Mathf.Sin(timeCounter) * height + startPoint.y;
     float z = startPoint.z;

     transform.position = new Vector3(x, y, z);
 }

}

spawn public class SpawnScript : MonoBehaviour { public Transform[] spawnPoints; public GameObject[] balloons;

 void Start()
 {
     StartSpawn();
 }

 void StartSpawn()
 {

     for (int i = 0; i < spawnPoints.Length; i++)
     {
         GameObject target = Instantiate(balloons[i], spawnPoints[i].position, Quaternion.identity);
         Rigidbody rbTarget = target.GetComponent<Rigidbody>();
         rbTarget.AddForce(Camera.main.transform.position * 2f);
     }
 }

}

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
1

Answer by OlleStarclassic · Oct 09, 2021 at 09:12 PM

I guess you should not move your targets every frame by updating its transform.position.

The AddForce to the rigidbody can’t update the targets position because your moving its position yourself every frame.

Maybe you can add movement to the target via its AddForce method only and not updating its position every frame?

Comment
Add comment · Show 2 · 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 Eno-Khaon · Oct 09, 2021 at 09:14 PM 0
Share

As a side note:

 rbTarget.AddForce(Camera.main.transform.position * 2f);

This won't be very effective at moving toward the camera. It should probably be something more like:

 // It'll be much more efficient to cache the main camera somewhere
 rbTarget.AddForce((target.transform.position - mainCam.transform.position).normalized * 2f);
avatar image javanullpointer · Oct 09, 2021 at 09:20 PM 0
Share

yess you're right. I'm updating my z position with my start position. so even though i've added force it updated itself as start position. so I've change my circular movement code like below and problem solved. silly me :)

from float z = startPoint.z; to float z = gameObject.transform.position.z;

thank you for your help!!

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

188 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

Related Questions

Limiting Speed on Two Axis 1 Answer

Movement Physics: Max Speed and Momentun 0 Answers

Enemy avoid other enemies while pursuing player 1 Answer

Moving Objects using the mouse only 0 Answers

Using Add Force for character control 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