# Hovering Tank

 0 I have a sci-fi themed tank that I need to make it look like it's hovering up and down through C# script. How do I go about applying force to the rigidbody giving it the impression that it's hovering up and letting gravity bring it back down again all without increasing or editing the collider box? Where do I start?EDITSo I was playing around with the code I gave in the link below to see if it was an all in one solution and I think my freak of nature gravity value (-500) is affecting the code badly and my craft still sticks to the ground does anyone have a clue?Converted Code: using UnityEngine; using System.Collections;public class ScifiHover : MonoBehaviour { float forwardPower; float steerPower; float landingPower; float jumpingPower; float hoverHeight; float stability = 1; public GameObject body; public float speedUpdate; private Vector3[] hitNormal = new Vector3[5]; private Quaternion rotation; private float increment; private Vector3[] lastNormals = new Vector3[5]; private bool physicsSetup = false; private Vector3 boxDim; private Vector3[] cornersPoint = new Vector3[5]; private Transform[] corners = new Transform[5]; private BoxCollider boxCollider; private float yBounce; private Vector3 lastPosition; private float distance; private Vector3 average; void Awake() { InitializePhysics(); } void Update() { CalculateSpeed(); } void FixedUpdate() { if (physicsSetup) { RaycastHit hit; for (int i = 0; i <= corners.Length - 1; i++) { if (Physics.Raycast(corners[i].position, -corners[i].up, out hit, hoverHeight + 100.0f)) { if (hit.collider.gameObject.tag == "Floor") { hitNormal[i] = body.transform.InverseTransformDirection(hit.normal); if (lastNormals[i] != hitNormal[i]) { increment = 0; lastNormals[i] = hitNormal[i]; } distance = hit.distance; if (hit.distance < hoverHeight) { constantForce.relativeForce = (-average + transform.up) * rigidbody.mass * jumpingPower * rigidbody.drag * Mathf.Min(hoverHeight, hoverHeight / distance); } else { constantForce.relativeForce = -(transform.up) * rigidbody.mass * landingPower * rigidbody.drag / Mathf.Min(hoverHeight, hoverHeight / distance); } } } } average = -(hitNormal[0] + hitNormal[1] + hitNormal[2] + hitNormal[3] + hitNormal[4]) / 2; if (increment != 1) { increment += 0.03f; } rotation = Quaternion.Slerp(body.transform.localRotation, Quaternion.Euler(average * Mathf.Rad2Deg), increment); Quaternion temp = rotation; temp.y = transform.up.y * Mathf.Deg2Rad; body.transform.localRotation = temp; float fwdForce = Input.GetAxis("Vertical") * forwardPower; rigidbody.AddForce(transform.forward * fwdForce); float steerForce = Input.GetAxis("Horizontal") * steerPower; rigidbody.AddTorque(transform.up * steerForce); } } void CalculateSpeed() { if (lastPosition != transform.position) { float distance = Vector3.Distance(transform.position, lastPosition); speedUpdate = (distance / 1000) / (Time.deltaTime / 3600); //Km/h } } void InitializePhysics() { //Store the box dimenssion of the hovering object. boxCollider = body.AddComponent(); boxDim = new Vector3(boxCollider.size.x * body.transform.localScale.x, boxCollider.size.y * body.transform.localScale.y, boxCollider.size.z * body.transform.localScale.z) * stability; cornersPoint[0] = new Vector3(transform.position.x - boxDim.x / 2, transform.position.y - boxDim.y / 2, transform.position.z + boxDim.z / 2); cornersPoint[1] = new Vector3(boxDim.x / 2 + transform.position.x, transform.position.y - boxDim.y / 2, transform.position.z + boxDim.z / 2); cornersPoint[2] = new Vector3(boxDim.x / 2 + transform.position.x, transform.position.y - boxDim.y / 2, transform.position.z - boxDim.z / 2); cornersPoint[3] = new Vector3(transform.position.x - boxDim.x / 2, transform.position.y - boxDim.y / 2, transform.position.z - boxDim.z / 2); cornersPoint[4] = transform.position; Destroy(boxCollider); for (int i = 0; i <= cornersPoint.Length - 1; i++) { GameObject stablePlatform = GameObject.CreatePrimitive(PrimitiveType.Sphere); stablePlatform.name = "StablePlatform" + "(" + i + ")"; stablePlatform.transform.parent = body.transform; stablePlatform.transform.localPosition = transform.InverseTransformPoint(cornersPoint[i]); corners[i] = stablePlatform.transform; Destroy(stablePlatform.GetComponent()); Destroy(stablePlatform.GetComponent()); } cornersPoint = null; physicsSetup = true; } } more ▼ asked May 15 '12 at 08:44 AM djdrool91 12 ● 7 ● 11 ● 11 I believe animation would work better and provide more predictable results in this case. May 15 '12 at 12:25 PM asafsitner add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 As asafsitner mentioned, it's best to do it via animation, as it allows you to add unsteady hovering which also depends on the current state of the tank (i.e. moving, shooting, standing, searching).You can always use Mathf.Cos or Mathf.Sin, as they will produce a periodical value which ranges from -1 to 1. more ▼ answered May 15 '12 at 12:35 PM Tseng 1.5k ● 6 ● 8 ● 23 I would love to do it through animation however the current circumstance is not allowing me to do so and thus has to be done by script. I'm not too good with C# and unity so if you could help me get started on how to use Mathf.Cos or Mathf.Sin I would really appreaciate it.What I'm trying to achieve is something like on this video (http://www.youtube.com/watch?v=JE0ZLq5bobc) however my controller and camera are all set I just simply need to make the tank hover up and down by time intervals. May 16 '12 at 02:46 AM djdrool91 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x4140
x291
x60
x55

asked: May 15 '12 at 08:44 AM

Seen: 1361 times

Last Updated: Feb 24 at 12:12 PM