- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
isometricLove · Oct 16, 2014 at 06:47 PM ·
suspension
Coding my sunspension Need HELP PLS
I am trying to make sunspension for box applaying force back if box to close to ground but it dosent work can anyone help, you can test this on box just add script and just // tmA and text rendering stuff or add 4 childs whit text render PTA PTB PTC PTD
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));
TextMesh tmA = (TextMesh)GameObject.Find("PTA").GetComponent<TextMesh>();
TextMesh tmB = (TextMesh)GameObject.Find("PTB").GetComponent<TextMesh>();
TextMesh tmC = (TextMesh)GameObject.Find("PTC").GetComponent<TextMesh>();
TextMesh tmD = (TextMesh)GameObject.Find("PTD").GetComponent<TextMesh>();
//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 CRatioA =hitColectorPointA.distance / RaycastDistance;
//Add UP force at point to lift thet side if its lower by any other force
rigidbody.AddForceAtPosition (Vector3.up * CRatioA * SunspensionPower, pointA, ForceMode.Force);
// GreenRay if hiting somthing
Debug.DrawRay (pointA, -transform.up * RaycastDistance, Color.green);
//Debug.Log (hitColectorPointA.distance);
var hitdisA = hitColectorPointA.distance;
string RounderA = string.Format("{0:0.##}", hitdisA);
tmA.text = RounderA;
} 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 CRatioB =hitColectorPointB.distance / RaycastDistance;
//Add UP force at point to lift thet side if its lower by any other force
rigidbody.AddForceAtPosition (Vector3.up * CRatioB * SunspensionPower, pointB, ForceMode.Force);
// GreenRay if hiting somthing
Debug.DrawRay (pointB, -transform.up * RaycastDistance, Color.green);
//Debug.Log (hitColectorPointB.distance);
var hitdisB = hitColectorPointA.distance;
string RounderB = string.Format("{0:0.##}", hitdisB);
tmB.text = RounderB;
} 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 CRatioC = hitColectorPointC.distance / RaycastDistance;
//Add UP force at point to lift thet side if its lower by any other force
rigidbody.AddForceAtPosition (Vector3.up * CRatioC * SunspensionPower, pointC, ForceMode.Force);
// GreenRay if hiting somthing
Debug.DrawRay (pointC, -transform.up * RaycastDistance, Color.green);
//Debug.Log (hitColectorPointC.distance);
var hitdisC = hitColectorPointC.distance;
string RounderC = string.Format("{0:0.##}", hitdisC);
tmC.text = RounderC;
} 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 CRatioD = (hitColectorPointD.distance / RaycastDistance);
//Add UP force at point to lift thet side if its lower by any other force
rigidbody.AddForceAtPosition (Vector3.up * CRatioD * SunspensionPower, pointD, ForceMode.Force);
// GreenRay if hiting somthing
Debug.DrawRay (pointD, -transform.up * RaycastDistance, Color.green);
//Debug.Log (hitColectorPointD.distance);
var hitdisD = hitColectorPointD.distance;
string RounderD = string.Format("{0:0.##}", hitdisD);
tmD.text = RounderD;
} else
{
//Red Ray if not hiting anything
Debug.DrawRay (pointD, -transform.up * RaycastDistance, Color.red);
}
}
}
Comment
i am trying to achive http://www.youtube.com/watch?v=LG1CtlFRmpU watch from 2:02
Your answer