如何改变镜头的路径? (2D)

时间:2019-04-16 17:36:59

标签: c# unity3d

我希望我的镜头遵循特定的模式(我还需要镜头之间的弧度和间隙可以调整)。现在我已经放下了射击脚本,但是射击不是我想要的直线(现在不想要直线,但是稍后在设计其他武器时将需要它)。
这是带有上述说出的示例的屏幕截图:

enter image description here

我对四元数和角度了解不多,所以我所尝试的只是修改x时间后的角度和x时间后的速度,但是没有用(可能是解决方案,但是我有0条线索如何统一使用角度所以我无法使用它。
另一件事,请提供解释以及您的答案,因为我想了解为什么某事物会以这种方式起作用,所以我以后不必再询问了。

这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class Player_Shooting : MonoBehaviour
{
    [SerializeField]
    private Transform shootingPoint;
    [SerializeField]
    private GameObject shot; //this is what I'm shooting, shot also has a script but all it does is apply velocity upwards and do damage to enemy if it hits
    private bool shootAgain = true;
    private int dexterity = Player_Stats.GetDexterity();
    private int numberofshots = 2; //amount of shots
    private int shotGap = 5; //how many degrees between the shots

    void Update()
    {
        Vector3 mousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2 direction = new Vector2(mousepos.x - transform.position.x, mousepos.y - transform.position.y);
        transform.up = direction;
        if (Input.GetButton("Fire1") && shootAgain == true)
        {
            shootAgain = false;
            StartCoroutine(RateOfFire(dexterity));
        }
    }

    private void Shoot()
    {
        Vector3 temp = transform.rotation.eulerAngles;
        Quaternion angle = Quaternion.Euler(temp.x, temp.y, temp.z);
        for (int i = 0; i < numberofshots; i++)
        {
            int multiplier = i + 1;
            if (numberofshots % 2 == 1)
            {
                Instantiate(shot, shootingPoint.position, angle);
                if (i % 2 == 0)
                {
                    temp.z -= shotGap * multiplier;
                    angle = Quaternion.Euler(temp.x, temp.y, temp.z);
                }
                else
                {
                    temp.z += shotGap * multiplier;
                    angle = Quaternion.Euler(temp.x, temp.y, temp.z);
                }
            }
            else if (numberofshots % 2 == 0)
            {
                if (i % 2 == 0)
                {
                    temp.z -= shotGap * multiplier;
                    angle = Quaternion.Euler(temp.x, temp.y, temp.z);
                }
                else
                {
                    temp.z += shotGap * multiplier;
                    angle = Quaternion.Euler(temp.x, temp.y, temp.z);
                }
                Instantiate(shot, shootingPoint.position, angle);
            }
        }
    }

    IEnumerator RateOfFire(int dex)
    {
        Shoot();
        float time = dex / 75;
        time *= 6.5f;
        time += 1.5f;
        yield return new WaitForSeconds(1 / time);
        shootAgain = true;
    }
}

1 个答案:

答案 0 :(得分:0)

这是我几个小时后想到的。 可以根据您的需要对其进行改进,但是可以用更少的代码来工作。 我在另一个gameObject上使用了一个单独的脚本来实例化弹丸。项目符号脚本附加到带有轨迹的精灵上 应该很容易从那里操纵点火顺序。

评论解释了大多数事情。 我添加了布尔功能以相反的角度射击。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class bullet : MonoBehaviour{
public float turnLength = 0.5f; // how long it turns for 0.0+
public float turnSpeed = 5f; // how fast the projectile turns 0.0+ 
public float anglePauseTime = 0.2f; // Optional wave form variable. coupled with high turnrate + curve speed = higher frequency sine wave.
public float shotAngle = -12f; // the angle the shot is taken as an offset (usually nagative value) 0- or turnspeed*2.25 for straight shots
public float projectileSpeed = 50; // obvious
public bool opositeAngles = false;

// Start is called before the first frame update
void Start(){
    if(opositeAngles){
        transform.Rotate(0, 0, -shotAngle);
    }
    else{
        transform.Rotate(0, 0, shotAngle);
    }

    StartCoroutine(WaveForm(turnLength, turnSpeed, anglePauseTime, opositeAngles));
}

// Update is called once per frame
void Update(){
    transform.position += transform.right * Time.deltaTime * projectileSpeed;
}

IEnumerator WaveForm(float seconds, float aglSpeed, float pause, bool reverse){
    // multiplier correlates to waitForSeconds(seconds)  
    // faster update time = smoother curves for fast projectiles
    // less cycles = shorter Corutine time. 
    //10, 0.1   100cycles/second (shallow waves, jagged on higher frequency waves, doesnt last long)
    //10, 0.05  200cycles/second (probably best)
    //100, 0.02 500cycles/second (smooth curves all around. requires smaller adjustment numbers)
    // i had to up it for the waveform to last longer. 
    float newSeconds = seconds * 10; 

    for (int i = 0; i < newSeconds; i++) {
        for (int j = 0; j < newSeconds; j++) {
            yield return new WaitForSeconds(0.05f); // controls update time in fractions of a second. 
            if(reverse){
                transform.Rotate(0, 0, -aglSpeed, Space.Self);
            }
            else {
                transform.Rotate(0, 0, aglSpeed, Space.Self);
            }
        }
        yield return new WaitForSeconds(pause);
        aglSpeed = -aglSpeed;
    }
}
}

Example image