Moving gameobject in a smooth circle around the player object

早过忘川 提交于 2020-01-16 04:53:08

问题


I'm using unity 5.1.2 if that makes a difference.

I have a game object shield, that I want to move in a circle around a player. I have it working to a degree, the shield responds well to input but is not animated in that it just teleports to the new position instead of moving around in a circle in a smooth rotation. The game is 2D top down so working in the x/y plane only. Have tried to use lerp and slerp but not getting any joy

Would really appreciate your help to figure this one out!

Here's what I have so far:

public class ShieldMovement : MonoBehaviour {

    public Transform target; //player shield is attaced to

    float distance = 0.8f; // distance from player so it doesn't clip
    Vector3 direction = Vector3.up;


    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

        float angle = Mathf.Atan2 (Input.GetAxisRaw("rightH"), Input.GetAxisRaw("rightV"))* Mathf.Rad2Deg;

        if(Input.GetAxis("rightH") != 0f || Input.GetAxis("rightV") != 0f)
        {
            direction =  new Vector3(Input.GetAxis("rightH"),Input.GetAxis("rightV"), 0.0f ) ;
        }

        Ray ray = new Ray(target.position, direction);
        transform.position = ray.GetPoint(distance);

        if(Input.GetAxis("rightH") != 0f || Input.GetAxis("rightV") != 0f)
        {
            transform.rotation = Quaternion.AngleAxis(angle,Vector3.forward*-1);
        }
    }
}

回答1:


Try this. With so many method calls to Input.GetAxis("...") you really want to just call it once in the beginning and cache it to a variable to speed things up. Also you don't really need to check if(Input.GetAxis("rightH") != 0f || Input.GetAxis("rightV") != 0f) twice, so i put everything into one of the checks. I added a multiplication by Time.smoothDeltaTime to hopefully smooth things out for you

float h = Input.GetAxis("rightH");
float v = Input.GetAxis("rightV");

float angle = Mathf.Atan2(h, v) * Mathf.Rad2Deg;

if(h != 0f || v != 0f)
{
    float step = Time.smoothDeltatime * speed;
    direction = Vector3.RotateTowards(transform.forward, new Vector2(h, v).normalized, step, 0.0F);
    Ray2D ray = new Ray2D(target.position, direction);
    transform.position = ray.GetPoint(distance);
    transform.rotation = Quaternion.AngleAxis(angle, -Vector3.forward);
}



回答2:


After much searching and a bit of help I finally solved the problems, thanks guys :) Here's the solution I came up with

public class ShieldMovement : MonoBehaviour {

public Transform target; //player shield is attaced to

float distance = 0.8f; // distance from player so it doesn't clip
Vector3 direction = Vector3.up;
public float circSpeed = 0.1f; // Speed Shield moves around ship


// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

    float rh = Input.GetAxisRaw("rightH");
    float rv = Input.GetAxisRaw("rightV");

    if(Mathf.Abs(rh) > 0.15f || Mathf.Abs(rv) > 0.15f)
    {
        Vector3 targetDir = new Vector3(rh, rv, 0.0f);
        direction  = Vector3.Slerp(transform.position-target.position, targetDir, circSpeed);

    }

    Ray ray = new Ray(target.position, direction); // cast ray in direction of point on circle shield is to go

    transform.position = ray.GetPoint(distance); //move shield to the ray as far as the radius

    float angleY = transform.position.y - target.position.y;
    float angleX = -(transform.position.x - target.position.x);

    float angle = Mathf.Atan2 (angleY, angleX) * Mathf.Rad2Deg-90; //Get angle

    if(Mathf.Abs(rh) > 0.15f || Mathf.Abs(rv) > 0.15f)
    {
        transform.rotation = Quaternion.AngleAxis(angle,Vector3.forward*-1); // keep shield facing outwards in respect to player, will need revisiting on animating shield properly
    }


}

}



来源:https://stackoverflow.com/questions/32314238/moving-gameobject-in-a-smooth-circle-around-the-player-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!