- Home /
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
Ok i figured out, i had a component ConstantForce2D Applied on my meteor ^^'
Your answer
![](https://koobas.hobune.stream/wayback/20220613011712im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Remove movement inertia 1 Answer
2D projectile trajectory prediction 2 Answers
2D bullet trajectory prediction (line renderer) 0 Answers
2D Keeping player stuck to a square platform 1 Answer
Change gravity on button 1 Answer