I need to make this work in FixedUpdate but it lags i dont get how to implement delta time so it beacomes smooth, anyone help?
using UnityEngine;
using System.Collections;
public class ReycastingSuspension : MonoBehaviour {
//The distance of ray
public float RaycastDistance;
//Variable to play whit in unity editor
public float SunspensionPower;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
//Difines botom corners so thet can later be used for raycasting
Vector3 pointA = transform.TransformPoint (new Vector3 (0.5f, -0.5f, 0.5f));
Vector3 pointB = transform.TransformPoint (new Vector3 (0.5f, -0.5f, -0.5f));
Vector3 pointC = transform.TransformPoint (new Vector3 (-0.5f, -0.5f, 0.5f));
Vector3 pointD = transform.TransformPoint (new Vector3 (-0.5f, -0.5f, -0.5f));
//defines HitColector and executes ray for PointA
RaycastHit hitColectorPointA;
if (Physics.Raycast (pointA, -transform.up, out hitColectorPointA, RaycastDistance)) {
//CRatio shoud be formula for how much ray is compresed
float CRatio = hitColectorPointA.distance / RaycastDistance;
//Add UP force at point to lift thet side if its lower by any other force
rigidbody.AddForceAtPosition (Vector3.up * CRatio * SunspensionPower, pointA, ForceMode.Force);
// GreenRay if hiting somthing
Debug.DrawRay (pointA, -transform.up * RaycastDistance, Color.green);
Debug.Log (hitColectorPointA.distance);
} else
{
//Red Ray if not hiting anything
Debug.DrawRay (pointA, -transform.up * RaycastDistance, Color.red);
}
//defines HitColector and executes ray for PointB
RaycastHit hitColectorPointB;
if (Physics.Raycast (pointB, -transform.up, out hitColectorPointB, RaycastDistance)) {
//CRatio shoud be formula for how much ray is compresed
float CRatio = hitColectorPointB.distance / RaycastDistance;
//Add UP force at point to lift thet side if its lower by any other force
rigidbody.AddForceAtPosition (Vector3.up * CRatio * SunspensionPower, pointB, ForceMode.Force);
// GreenRay if hiting somthing
Debug.DrawRay (pointB, -transform.up * RaycastDistance, Color.green);
Debug.Log (hitColectorPointB.distance);
} else
{
//Red Ray if not hiting anything
Debug.DrawRay (pointB, -transform.up * RaycastDistance, Color.red);
}
//defines HitColector and executes ray for PointC
RaycastHit hitColectorPointC;
if (Physics.Raycast (pointC, -transform.up, out hitColectorPointC, RaycastDistance)) {
//CRatio shoud be formula for how much ray is compresed
float CRatio = hitColectorPointC.distance / RaycastDistance;
//Add UP force at point to lift thet side if its lower by any other force
rigidbody.AddForceAtPosition (Vector3.up * CRatio * SunspensionPower, pointC, ForceMode.Force);
// GreenRay if hiting somthing
Debug.DrawRay (pointC, -transform.up * RaycastDistance, Color.green);
Debug.Log (hitColectorPointC.distance);
} else
{
//Red Ray if not hiting anything
Debug.DrawRay (pointC, -transform.up * RaycastDistance, Color.red);
}
//defines HitColector and executes ray for PointD
RaycastHit hitColectorPointD;
if (Physics.Raycast (pointD, -transform.up, out hitColectorPointD, RaycastDistance)) {
//CRatio shoud be formula for how much ray is compresed
float CRatio = hitColectorPointD.distance / RaycastDistance;
//Add UP force at point to lift thet side if its lower by any other force
rigidbody.AddForceAtPosition (Vector3.up * CRatio * SunspensionPower, pointD, ForceMode.Force);
// GreenRay if hiting somthing
Debug.DrawRay (pointD, -transform.up * RaycastDistance, Color.green);
Debug.Log (hitColectorPointD.distance);
} else
{
//Red Ray if not hiting anything
Debug.DrawRay (pointD, -transform.up * RaycastDistance, Color.red);
}
}
}
//Debug.LogWarning("WORKING")