Farhan Ali
Farhan Ali

Reputation: 509

Need local transforms, from global transforms (C#) Unity 3d

I have a missile effect, when I fired to global gameobject as a target, it works fine, but I fired to any gameobject which is in some parent, it doesn't work fine, it goes to an unknown position. Here is my code to move missile to the target.

m_target = is my target
m_targetOffset = if to need to add any offset, by default it is (0,0,0)

protected override void goToTarget()
{       

    m_direction = (m_target.position + m_targetOffset - transform.position).normalized * m_distanceInfluence.Evaluate(1 - (m_target.position + m_targetOffset - transform.position).magnitude / m_searchRange);
    m_rigidbody.velocity = Vector3.ClampMagnitude(m_rigidbody.velocity + m_direction * m_guidanceIntensity, m_rigidbody.velocity.magnitude);

    if (m_rigidbody.velocity != Vector3.zero)
        transform.LookAt(m_rigidbody.velocity);
}

Upvotes: 0

Views: 96

Answers (1)

BugFinder
BugFinder

Reputation: 17858

In your code, it probably doesnt work because m_target is the gameobject, not the transform, therefore instead of m_target.position, its m_target.transform.position

Upvotes: 1

Related Questions