Hi, I just started making a game, it is in 2d and relies on physics, and my objects always have crazy jitter when colliding and I dont know what to do
here are my settings for the objects
the jittering only happens when i move the spaceship, when it doesnt move its fine
here is the code for the asteroids and the spaceship
spaceship:
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
private float screenHeight;
private float screenWidth;
private float objectHeight;
private float objectWidth;
private SpriteRenderer spriteRenderer;
void Start()
{
Camera camera = Camera.main;
spriteRenderer = GetComponent();
screenHeight = camera.orthographicSize;
screenWidth = screenHeight * camera.aspect;
objectHeight = spriteRenderer.bounds.size.y / 2;
objectWidth = spriteRenderer.bounds.size.x / 2;
}
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(horizontalInput, verticalInput, 0) * moveSpeed * Time.deltaTime;
Vector3 newPosition = transform.position + movement;
newPosition.x = Mathf.Clamp(newPosition.x, -screenWidth + objectWidth, screenWidth - objectWidth);
newPosition.y = Mathf.Clamp(newPosition.y, -screenHeight + objectHeight, screenHeight - objectHeight);
transform.position = newPosition;
}
}
asteroids:
using UnityEngine;
using System.Collections;
public class RandomPassThroughSpawner : MonoBehaviour
{
public GameObject objectPrefab;
public float maxSpeed = 15f;
public float minSpeed = 100f;
public float spawnDelay = 0.3f;
public float minSize = 1f;
public float maxSize = 5f;
private float screenWidth;
private float screenHeight;
private float offScreenMargin = 1.5f;
void Start()
{
Camera camera = Camera.main;
screenHeight = camera.orthographicSize;
screenWidth = screenHeight * camera.aspect;
// Start the spawning coroutine
StartCoroutine(SpawnObject());
}
IEnumerator SpawnObject()
{
while (true)
{
GameObject newObject = Instantiate(objectPrefab);
int spawnSide = Random.Range(0, 4);
Vector3 spawnPosition = Vector3.zero;
switch (spawnSide)
{
case 0: // Top
spawnPosition = new Vector3(Random.Range(-screenWidth, screenWidth), screenHeight * offScreenMargin, -3);
break;
case 1: // Bottom
spawnPosition = new Vector3(Random.Range(-screenWidth, screenWidth), -screenHeight * offScreenMargin, -3);
break;
case 2: // Left
spawnPosition = new Vector3(-screenWidth * offScreenMargin, Random.Range(-screenHeight, screenHeight), -3);
break;
case 3: // Right
spawnPosition = new Vector3(screenWidth * offScreenMargin, Random.Range(-screenHeight, screenHeight), -3);
break;
}
float objectSize = Random.Range(minSize, maxSize);
newObject.transform.localScale = new Vector3(objectSize, objectSize, objectSize);
newObject.transform.position = spawnPosition;
float randomAngle = Random.Range(0f, 360f); // Angle in degrees
Vector3 randomDirection = new Vector3(Mathf.Cos(randomAngle * Mathf.Deg2Rad), Mathf.Sin(randomAngle * Mathf.Deg2Rad), 0);
float randomSpeed = Random.Range(minSpeed, maxSpeed);
Rigidbody2D objectRb = newObject.GetComponent<Rigidbody2D>();
objectRb.linearVelocity = randomSpeed * randomDirection;
yield return new WaitForSeconds(spawnDelay);
}
}
}
pls help