ConstantVelocity

From Unify Community Wiki
Jump to: navigation, search

Description

Useful for things like missiles, bullets, etc. You can also alternatively just attach the built-in ConstantForce component to a GameObject.

CSharp - ConstantVelocity.cs

using UnityEngine;
 
[RequireComponent (typeof (Rigidbody))]
public class ConstantVelocity : MonoBehaviour
{
    public Vector3 direction = Vector3.forward;
 
    private Rigidbody m_Rigidbody;
    private Transform m_Transform;
 
 
    private void Awake ()
    {
        // Optimization - Cache component references.
        m_Transform = GetComponent<Transform> ();
        m_Rigidbody = GetComponent<Rigidbody> ();
    }
 
 
    private void FixedUpdate ()
    {
        m_Rigidbody.velocity = m_Transform.rotation * direction;
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox