im trying to get my gravity to effect my plane harder if the nose is flying straight up and hitting the collider, its bouncing off nicely at an angle but gets stuck when flying straight up, i want to increase the gravity if its hitting with the nose straight up so it will push the plane away more, would also be nice to lower the nose a bit when it stalls, but i cant work out how to do this. the rot and rotate variables im using for control dont seem to be correct.
here is da code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlaneController : MonoBehaviour
{
Rigidbody2D rb;
[Tooltip("World units per second.")]
public float moveSpeed;
[Tooltip("Degrees per second.")]
public float rotateAmount;
float rot;
bool stalling = false;
private Renderer[] renderers;
private bool isWrappingX = false;
SpriteRenderer spriteRenderer;
void Start()
{
renderers = GetComponentsInChildren<Renderer>();
}
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = 1; // gotta cast a little bit into the scene!
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
if (mousePos.x < 0)
{
rot = rotateAmount;
}
else
{
rot = -rotateAmount;
}
transform.Rotate(0, 0, rot * Time.deltaTime);
}
else
{
SetFlipY();
}
if (stalling)
{
rb.gravityScale = 15;
stalling = false;
StartCoroutine(StallTime());
if (transform.right.x > 0 && transform.right.x < 5) rb.gravityScale = 50;
}
}
private void FixedUpdate()
{
rb.velocity = transform.right * moveSpeed;
ScreenWrap();
}
void SetFlipY()
{
// I'm not going to base it off of rot but rather off of the
// sign of the x component of the transform.right vector.
bool flipy = transform.right.x < 0;
spriteRenderer.flipY = flipy;
}
void ScreenWrap()
{
bool isVisible = CheckRenderers();
if (isVisible)
{
isWrappingX = false;
return;
}
if (isWrappingX)
{
return;
}
Vector3 newPosition = transform.position;
if (newPosition.x > 1 || newPosition.x < 0)
{
newPosition.x = -newPosition.x;
isWrappingX = true;
}
transform.position = newPosition;
}
bool CheckRenderers()
{
foreach (Renderer renderer in renderers)
{
if (renderer.isVisible)
{
return true;
}
}
return false;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "HeightBorder")
{
stalling = true;
}
}
IEnumerator StallTime()
{
//float randomTime = Random.Range(3f, 10f);
yield return new WaitForSeconds(0.5f);
rb.gravityScale = 0;
}
}