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 Siliko · Sep 16, 2020 at 07:55 PM · 2dgravitytrajectory

Trajectory2D not accurate

Hello,

I'm spawning a meteor around a planet on which i apllied a script to get rotational gravity. I apply a force to the meteor on spawn and try to get its trajectory. I putted gravity scale and drag to 0 and mass to 1. But the trajectory is not accurated. Here's my code, sorry for useless variables sometimes, i'm trying to figuring out :

Code of Gravity

   using System.Collections;
     using System.Collections.Generic;
     using System.Diagnostics;
     using UnityEngine;
 
     public class Gravity : MonoBehaviour
 
     {
         Rigidbody2D rb;
     Vector2 lookDirection;
 
     float lookAngle;
 
     [Header("Gravity")]
 
     // Distance where gravity works
     [Range(0.0f, 1000.0f)]
     public float maxGravDist = 150.0f;
 
     // Gravity force
     [Range(0.0f, 1000.0f)]
     public float maxGravity = 150.0f;
 
     // Your planet
     public GameObject planet;
 
 
 void Start()
 {
     rb = GetComponent<Rigidbody2D>();
 }
 
 void FixedUpdate()
 {
         //if (!(Input.GetMouseButton(0))) // Does not affect the comet while the player is still touching the screen
        // {
             // Distance to the planet
     float dist = Vector2.Distance(planet.transform.position, transform.position);
 
         // Gravity
     Vector2 v = planet.transform.position - transform.position;
     rb.AddForce(v.normalized* (1.0f - dist / maxGravDist) * maxGravity);
 
 
         // Rotating to the planet   Pas besoin en 2D je crois
         //lookDirection = planet.transform.position - transform.position;
         //lookAngle = Mathf.Atan2(lookDirection.y, lookDirection.x) * Mathf.Rad2Deg;
 
         //transform.rotation = Quaternion.Euler(0f, 0f, lookAngle);
         //}
     }
 }

Code of spawning Meteor :

 using Microsoft.Win32;
 using System.CodeDom;
 using System.Collections;
 using System.Collections.Generic;
 using System.Collections.Specialized;
 using System.Diagnostics;
 using System.Runtime.CompilerServices;
 using System.Security.Cryptography;
 using System.Security.Cryptography.X509Certificates;
 using UnityEngine;
 
 public class Spawn_Object : MonoBehaviour
 {
 
     public Rigidbody2D prefab;
     public Rigidbody2D clone;
     public LineRenderer lr;
 
     // Your planet
     public GameObject planet;
     public Vector2 position;
     private Vector2 velocity;
     private Vector3 pos;
     private Vector2 power;
 
 
 
     public Vector2 worldPosition;
 
 
     // Update is called once per frame
     void Update()
     {
 
         if (Input.GetMouseButtonDown(0))
         {
 
             worldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             //DragStartPos = worldPosition;
 
 
             clone = Instantiate(prefab, worldPosition, Quaternion.identity);
             clone.constraints = RigidbodyConstraints2D.FreezeAll;
 
             lr = clone.GetComponent<LineRenderer>();
 
             
 
 
 
         }
 
         if (Input.GetMouseButton(0))
         {
 
             velocity.x = 0;
             velocity.y = 0;
             
 
             position = worldPosition;
             power = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             power = worldPosition - power ;
             power *= 100f;
             
             UnityEngine.Debug.Log(power);
             velocity += power;
             Vector3 posActual = worldPosition;
             
             for (int i = 0; i < 200; ++i)
             {
                 pos = position;
                 posActual = pos;
                 lr.SetPosition(i, pos);
                 float dist = Vector2.Distance(planet.transform.position, posActual);
                 Vector2 v = (planet.transform.position - posActual);
                 Vector2 AccOfGravity = (v.normalized * (1.0f - dist / 150) * 150);
                 velocity += AccOfGravity * Time.fixedDeltaTime;
 
                 position += velocity * Time.fixedDeltaTime; // move point
                 //UnityEngine.Debug.Log(position);
                 // Plot the predicted point visually
 
                 
                 
                 
             }
 
 
         }
 
         if (Input.GetMouseButtonUp(0))
         {
             //Vector2 DragEndPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             //Vector2 _velocity = (DragEndPosition - DragStartPos) * power;
 
             //clone.velocity = _velocity;
 
             UnityEngine.Debug.Log(Time.fixedDeltaTime);
             clone.constraints = RigidbodyConstraints2D.None;
             clone.velocity += power;
         }
     }
 
 
     }






Thanks for your help :)

Comment
Add comment · Show 1
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 Siliko · Sep 12, 2020 at 09:30 PM 0
Share

Ok i figured out, i had a component ConstantForce2D Applied on my meteor ^^'

0 Replies

· Add your reply
  • Sort: 

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

295 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Need help on my script to calculate gravity for a mass 1 Answer

Add force to player at a certain z angle in 2D 1 Answer

Can I access Physics2dSettings in Project Settings via script 1 Answer

Instantiated prefab changes gravitational speed when dragged? 0 Answers

how to jump at fixed height but faster 0 Answers


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