コード例 #1
0
    // Update is called once per frame
    void Update()
    {
        Debug.Log("Angle between planet and player is " + Vector3.Angle(planet.transform.position, player.transform.position));
        Debug.Log("Angle between planet and enemy is " + Vector3.Angle(planet.transform.position, transform.position));
        float sphereAngle = angleBetweenTwoPointsOnSphere(transform.position, player.transform.position, planet.transform.position);

        Debug.Log("Sphere angle is " + sphereAngle);
        if (sphereAngle <= 20.0f)
        {
            Plane2  plane       = new Plane2(transform.parent.up, transform.position);
            Vector2 mappedPoint = plane.GetMappedPoint(player.transform.position) - plane.GetMappedPoint(transform.position);
            //Debug.Log("mappedPoint x is " + mappedPoint.x + " and mappedPoint y is " + mappedPoint.y);
            //Debug.Log("Unmapped point is " + (mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir));
            Debug.DrawLine(transform.position, transform.position + mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir, Color.red);
            if (mappedPoint.magnitude < 10f)
            {
                Debug.Log("Magnitiude tiny");
                return;
            }
            float angle = (float)(Math.Atan2((transform.position + mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir).z - transform.position
                                             .z, (transform.position + mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir).x - transform.position.x) * 180 / Math.PI);
            //Debug.Log("Calculated angle is " + angle + " and Vector3 angle is " + Vector3.Angle(transform.position, player.transform.position));
            //transform.rotation = Quaternion.Euler(0f, 270 - angle, 0f);
            //transform.localRotation = Quaternion.AngleAxis(angle, player.transform.position);
        }
    }
コード例 #2
0
    public void golemSphereMovement()
    {
        //This code moves the golem towards the player using plane-logic
        //For shrab, instead of transform.position.normalized, raycast to the center of the planet and use hit.normal
        Plane2 plane = new Plane2(transform.position.normalized, transform.position);

        Vector2 mappedPoint = plane.GetMappedPoint(player.transform.position) - plane.GetMappedPoint(transform.position);

        if (mappedPoint.magnitude > 1)
        {
            transform.LookAt(mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir + transform.position, transform.position.normalized);
        }
        RaycastHit hit;

        if (Physics.Raycast(transform.position, (planet.transform.position - transform.position).normalized, out hit, Mathf.Infinity))
        {
            Plane2 alignPlane = new Plane2(hit.normal, transform.position);
            //Vector2 mappedPoint2 = alignPlane.GetMappedPoint(player.transform.position) - alignPlane.GetMappedPoint(transform.position);
            //rb.AddForce((mappedPoint2.x * alignPlane.xDir + mappedPoint2.y * alignPlane.yDir).normalized * speed);
            if (Vector3.Distance(hit.point, transform.position) >= 1f)
            {
                rb.AddForce(transform.position.normalized * gravity * 2);
                //rb.AddForce((mappedPoint2.x * alignPlane.xDir + mappedPoint2.y * alignPlane.yDir).normalized * speed/-2f);
            }
        }
        //adding force towards gravity, adding force towards direction faced
        rb.AddForce(transform.forward * speed);

        rb.AddForce(transform.position.normalized * gravity);
    }
コード例 #3
0
    public void orientEnemy(Vector3 target)
    {
        Plane2  plane         = new Plane2(transform.position.normalized, transform.position);
        Vector2 mappedPoint   = plane.GetMappedPoint(target) - plane.GetMappedPoint(transform.position);
        Vector3 mappedPoint3D = mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir;

        Debug.Log("Mapped point magnitude is " + mappedPoint.magnitude);
        if (mappedPoint.magnitude > 1)
        {
            transform.LookAt(mappedPoint3D + transform.position, transform.position.normalized);
        }
    }
コード例 #4
0
    public void shrabSphereMovement(Vector3 target)
    {
        //This code moves the golem towards the player using plane-logic
        //For shrab, instead of transform.position.normalized, raycast to the center of the planet and use hit.normal
        Vector3    targetPosition          = findRaycastPointOnSphere(target);
        Vector3    planeInstantiationPoint = Vector3.zero;
        RaycastHit planeHit;

        if (Physics.Raycast(transform.position.normalized * 5f + transform.position, (-transform.position).normalized, out planeHit, Mathf.Infinity))
        {
            planeInstantiationPoint = planeHit.normal;
            //if(Vector3.Distance(transform.position, planeHit.point) >= 2.0f)
            //{
            transform.position = planeHit.point + transform.position.normalized * distanceAbovePlanetSurface;
            //}
        }

        Debug.Log("Plane instantiation point is " + planeInstantiationPoint);
        Plane2 plane = new Plane2(planeInstantiationPoint, transform.position);

        Vector2 mappedPoint = plane.GetMappedPoint(target) - plane.GetMappedPoint(transform.position);

        if (mappedPoint.magnitude > 1)
        {
            transform.LookAt(mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir + transform.position, transform.position.normalized);
        }

        /*
         * RaycastHit hit;
         * if (Physics.Raycast(transform.position, (planet.transform.position - transform.position).normalized, out hit, Mathf.Infinity))
         * {
         *  Plane2 alignPlane = new Plane2(hit.normal, transform.position);
         *  //Vector2 mappedPoint2 = alignPlane.GetMappedPoint(player.transform.position) - alignPlane.GetMappedPoint(transform.position);
         *  //rb.AddForce((mappedPoint2.x * alignPlane.xDir + mappedPoint2.y * alignPlane.yDir).normalized * speed);
         *  if (Vector3.Distance(hit.point, transform.position) >= 1f)
         *  {
         *      rb.AddForce(transform.position.normalized * gravity * 2);
         *      //rb.AddForce((mappedPoint2.x * alignPlane.xDir + mappedPoint2.y * alignPlane.yDir).normalized * speed/-2f);
         *  }
         * }
         */
        //adding force towards gravity, adding force towards direction faced
        //float step = Time.deltaTime * speed;
        rb.AddForce((mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir).normalized * speed);
        //transform.position = Vector3.MoveTowards(transform.position, (mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir), step);
        //rb.velocity = (mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir) * speed;
        //rb.AddForce((mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir) * speed);

        //rb.AddForce(transform.position.normalized * gravity * rb.mass);

        Debug.DrawLine(transform.position, transform.position + mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir, Color.red);
    }
コード例 #5
0
    //Method is not in use right now
    public void Attract()
    {
        //Debug.Log("In attract function");
        Vector3 gravityUp = (transform.position - planet.transform.position).normalized;
        Vector3 localUp   = transform.up;
        //Quaternion targetRotation = Quaternion.FromToRotation(Vector3.up, transform.position.normalized);
        //Quaternion finalRotation = Quaternion.RotateTowards(transform.rotation, targetRotation, float.PositiveInfinity);
        // Debug.Log("Final rotation is " + finalRotation.eulerAngles);
        //transform.rotation = finalRotation;

        Plane2  plane       = new Plane2(transform.position.normalized, transform.position);
        Vector2 mappedPoint = plane.GetMappedPoint(player.transform.position) - plane.GetMappedPoint(transform.position);

        transform.LookAt(mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir + transform.position, transform.position.normalized);
        //float angle = (float)(Math.Atan2(mappedPoint.y, mappedPoint.x) * 180 / Math.PI);
        //Debug.Log("Calculated angle is " + angle + " and Vector3 angle is " + Vector3.Angle(transform.position, player.transform.position));
        // transform.localRotation = Quaternion.Euler(0f, angle, 0f);

        //rb.rotation = Quaternion.FromToRotation(localUp, gravityUp) * rb.rotation;
    }