Last active
June 21, 2017 08:19
-
-
Save keiranlovett/91ed244671c5df526e3ddb2e690feb55 to your computer and use it in GitHub Desktop.
Rigidbodies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
// Use for providing that initial oomph, like a cannonball launch, thrown rock or | |
// paper airplane. Contrast with the built-in ConstantForce, which applies force | |
// each update and would be more suitable for a self-powered rigidbody like a | |
// missile, ship, or simulation of gravity. -- The author | |
[RequireComponent (typeof(Rigidbody))] | |
public class InitialRigidbodyForce : MonoBehaviour { | |
public Vector3 force; | |
public Vector3 relativeForce; | |
public Vector3 torque; | |
public Vector3 relativeTorque; | |
void Start () { | |
rigidbody.AddForce(force); | |
rigidbody.AddRelativeForce(relativeForce); | |
rigidbody.AddTorque(torque); | |
rigidbody.AddRelativeTorque(relativeTorque); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
// If a rigidbody is facing a target (RigidbodyLookAtTransform) and powering toward its goal | |
// (ConstantForce - relative forward thrust), you've got a pretty good missile. But it | |
// will likely still have a bit of sideways-drift that may cause it to miss its target, | |
// especially when the target is trying to evade. There's where this script comes in. | |
// | |
// Place on the same object as a RigidbodyLookAtTransform to compensate for sideways-drift | |
// or target evasion. After a little tweaking, your missile won't miss. | |
// | |
// Keep in mind though, it's often more fun for the player when the missile sometimes misses. | |
// Therein is the magic that keeps us returning to physics-simulations. | |
// | |
// - The author | |
[RequireComponent (typeof(RigidbodyLookAtBase))] | |
public class RigidbodyCourseCorrector : MonoBehaviour { | |
public float compensationForce = 4f; | |
private RigidbodyLookAtTransform rigidbodyLookAtTransform; | |
void Start () { | |
rigidbodyLookAtTransform = GetComponent<RigidbodyLookAtTransform>(); | |
} | |
void FixedUpdate () { | |
Vector3 targetVector = (rigidbodyLookAtTransform.target.position - transform.position).normalized; | |
Vector3 offCourseVector = targetVector - rigidbody.velocity.normalized; | |
rigidbody.AddForce(offCourseVector * compensationForce); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class RigidbodyFloatingController : MonoBehaviour { | |
private const string HORIZONTAL_AXIS = "Horizontal"; | |
private const string VERTICAL_AXIS = "Vertical"; | |
private const string JUMP_BUTTON = "Jump"; | |
public float AdvanceForce = 200f; | |
public float SidestepForce = 200f; | |
public float JumpForce = 200f; | |
public float HoverThrottleDecay = 0.97f; | |
public float HoverMinThrottle = 0.5f; | |
private float hoverThrottle = 0f; | |
void FixedUpdate() { | |
AdjustHoverThrottle(); | |
ApplyForceToRigidbody(); | |
} | |
private void AdjustHoverThrottle() { | |
hoverThrottle = (hoverThrottle * HoverThrottleDecay) + ((1 - HoverThrottleDecay) * HoverIntention); | |
hoverThrottle = Mathf.Max(hoverThrottle, HoverMinThrottle); | |
} | |
private void ApplyForceToRigidbody() { | |
var force = ForwardForce + HorizontalForce + VerticalForce; | |
rigidbody.AddForce(force); | |
} | |
private float HoverIntention { | |
get { return Input.GetButton(JUMP_BUTTON) ? 1f : 0f; } | |
} | |
private Vector3 ForwardForce { | |
get { | |
var forward = Camera.main.transform.forward; | |
var input = Input.GetAxis(VERTICAL_AXIS); | |
return forward * input * AdvanceForce; | |
} | |
} | |
private Vector3 HorizontalForce { | |
get { | |
var right = Camera.main.transform.right; | |
var input = Input.GetAxis(HORIZONTAL_AXIS); | |
return right * input * SidestepForce; | |
} | |
} | |
private Vector3 VerticalForce { | |
get { | |
var up = Camera.main.transform.up; | |
return up * hoverThrottle * JumpForce; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
// Keep in mind that you can also increase the angular drag on your | |
// rigidbody. I often find the default setting to be too low for | |
// my needs. - The author | |
[RequireComponent (typeof(Rigidbody))] | |
public class RigidbodyKeepUpright : RigidbodyLookAtBase { | |
void Update () { | |
LookAt(transform.up, transform.position + (Vector3.up * 10)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
[RequireComponent (typeof(Rigidbody))] | |
public class RigidbodyLookAtBase : MonoBehaviour { | |
public float force = 0.1f; | |
public void LookAt(Vector3 facingDirection, Vector3 targetPosition) { | |
Vector3 relativeDirection = targetPosition - transform.position; | |
float degreesBetweenFacingDirectionAndTarget = Vector3.Angle(facingDirection, relativeDirection); | |
Vector3 crossProduct = Vector3.Cross(facingDirection, relativeDirection); | |
Vector3 torque = crossProduct * degreesBetweenFacingDirectionAndTarget * force; | |
rigidbody.AddTorque(torque); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
// Keep in mind that you can also increase the angular drag on your | |
// rigidbody. I often find the default setting to be too low for | |
// my needs. - The author | |
[RequireComponent (typeof(Rigidbody))] | |
public class RigidbodyLookAtTarget : RigidbodyLookAtBase { | |
public Vector3 target; | |
void Update () { | |
LookAt(transform.forward, target); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
// Keep in mind that you can also increase the angular drag on your | |
// rigidbody. I often find the default setting to be too low for | |
// my needs. - The author | |
[RequireComponent (typeof(Rigidbody))] | |
public class RigidbodyLookAtTransform : RigidbodyLookAtBase { | |
public Transform target; | |
void Update () { | |
LookAt(transform.forward, target.position); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment