Friday, March 14, 2025
HomeApp Securityunity - 3D Darkish Souls lock-on code: Participant strikes round goal in...

unity – 3D Darkish Souls lock-on code: Participant strikes round goal in a spiral, not good circle


I am engaged on a 3D sport in Unity that requires lock-on, or Z, concentrating on present in video games like Darkish Souls and Legend of Zelda: Ocarina of Time.

The code that I’ve achieves my meant objectives pretty carefully, however because the title states, the participant strikes across the goal in a spiral sample, moderately than an ideal circle: After every revolution across the goal, the participant will get additional and additional away as an alternative of remaining on the similar distance.

I attempted many various proposed options and seemed throughout, however nothing fastened this challenge; please advise:

        utilizing System;
        utilizing UnityEngine;
      
        public class Motion : MonoBehaviour
        {
            inner Rigidbody rb;
      
            personal float x, y, z;
      
            //Float Scalar for the Vector Motion Velocity
            public float vel;
            personal float angleVel;
      
            personal float hAxis, vAxis;
      
            inner Vector3 moveVec;
      
            //Boolean to manage whether or not or not Motion is feasible, primarily based on different states.
            public bool bCanWalk;
        
            //Enemy to which the participant is locked-on.
           
    public GameObject goal;

    inner Vector3 targetterToTarget;

    personal Vector3 crossProd;
      
            void Begin()
             RigidbodyConstraints.FreezeRotationZ;
            

            void Replace()
            {
                //Transfer as soon as per body
                Transfer();
            }
      
            void Transfer()
            {
                y = rb.velocity.y;
        hAxis = Enter.GetAxisRaw("Horizontal");
        vAxis = Enter.GetAxisRaw("Vertical");

        targetterToTarget = rework.place - goal.rework.place;
        crossProd = MathHelper.CrossProd(goal.rework.up.normalized, targetterToTarget.normalized);

        swap (hAxis)
        {
            case -1:
            case 1:
                x = crossProd.x * -hAxis * vel;
                z = crossProd.z * -hAxis * vel;
                break;
            default:
                angleVel = 0;
                x = 0;
                z = vAxis * vel;
                break;
        }

        SetMoveVec();


        rb.velocity = moveVec;
            }
      
            personal void SetMoveVec()
           {
                moveVec.x = x;
                moveVec.y = y;
                moveVec.z = z;
            }
        }

utilizing System;
utilizing UnityEngine;

public static class MathHelper
{
    public static Vector3 CrossProd(Vector3 targetForwardNormalized, Vector3 targetterToTargetNormalized)
    {
        Vector3 crossProdVar;

        crossProdVar.x =  (targetForwardNormalized.y * targetterToTargetNormalized.z) -
                          (targetForwardNormalized.z * targetterToTargetNormalized.y);

        crossProdVar.y =  (targetForwardNormalized.z * targetterToTargetNormalized.x) -
                          (targetForwardNormalized.x * targetterToTargetNormalized.z);

        crossProdVar.z =  (targetForwardNormalized.x * targetterToTargetNormalized.y) -
                          (targetForwardNormalized.y * targetterToTargetNormalized.x);


        return crossProdVar;
    }
}

Thanks.



Supply hyperlink

RELATED ARTICLES

Most Popular

Recent Comments