New to C#, trying to learn but its a slow process
Currently working on a project where I have a GravityBody
script (someone else wrote it) that applies gravity to a player character based on the direction and strength of gravity in different areas. I want to make the gravity force (GRAVITY_FORCE
) change depending on the current gravity area the player is in.
GravityBody
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class GravityBody : MonoBehaviour
{
private static float GRAVITY_FORCE = 800;
public Vector3 GravityDirection
{
get
{
if (_gravityAreas.Count == 0) return Vector3.zero;
_gravityAreas.Sort((area1, area2) => area1.Priority.CompareTo(area2.Priority));
return _gravityAreas.Last().GetGravityDirection(this).normalized;
}
}
private Rigidbody _rigidbody;
private List<GravityArea> _gravityAreas;
void Start()
{
_rigidbody = transform.GetComponent<Rigidbody>();
_gravityAreas = new List<GravityArea>();
}
void FixedUpdate()
{
_rigidbody.AddForce(GravityDirection * (GRAVITY_FORCE * Time.fixedDeltaTime), ForceMode.Acceleration);
Quaternion upRotation = Quaternion.FromToRotation(transform.up, -GravityDirection);
Quaternion newRotation = Quaternion.Slerp(_rigidbody.rotation, upRotation * _rigidbody.rotation, Time.fixedDeltaTime * 3f);;
_rigidbody.MoveRotation(newRotation);
}
public void AddGravityArea(GravityArea gravityArea)
{
_gravityAreas.Add(gravityArea);
}
public void RemoveGravityArea(GravityArea gravityArea)
{
_gravityAreas.Remove(gravityArea);
}
}
looking to add a script that I can apply to the gravity collision meshes that will change GRAVITY_FORCE
Some sort of slider to dictate the force would be ideal (not necessary though)
If anyone has any insights or suggestions it would be massively appreciated