简体   繁体   English

在Unity中旋转然后沿随机方向移动对象

[英]Rotate then move object in Random direction in Unity

I am trying to create a script in Unity that when another object touches it, it grabs the object, rotates it in a random direction, and launches it, but I only want to rotate and launch the other object on the z axis. 我试图在Unity中创建一个脚本,当另一个对象触摸它时,它将抓住该对象,以随机方向旋转它,然后启动它,但是我只想旋转并在z轴上启动另一个对象。

Here is my script: 这是我的脚本:

public BallController ballController;
private GameObject ball;
private Rigidbody2D ballRb;

 void OnTriggerEnter2D(Collider2D other)
{
    ball = other.gameObject;
    ballRb = ball.GetComponent<Rigidbody2D>();
    ball.transform.position = this.transform.position;
    // Freeze the ball
    ballRb.velocity = Vector2.zero;
    // Rotate the ball
    ball.transform.rotation = Quaternion.Euler(0, 0, Random.Range(0, 360));
    // Start moving the ball again
    ballRb.velocity = ballRb.velocity * ballController.bulletSpeed * Time.deltatime;

}

The other script (The ball) is has a Ridgidbody and is launched by another object into this one, the script gets the ball to rotate how I want to, but it won't get the ball moving again. 另一个脚本(球)具有Ridgidbody,并由另一个对象启动到该脚本中,该脚本使球旋转到我想要的方向,但不会使球再次移动。

The ballController is set in the editor and the bulletSpeed is just an int that I want the ball to travel at (currently set to 6). ballController是在编辑器中设置的,bulletSpeed只是我希望球以其移动的整数(当前设置为6)。

When dealing with Rigidbody2D you shouldn't use the transform for setting the rotation but rather use ballRb.rotation . 处理Rigidbody2D ,不应使用transform来设置旋转,而应使用ballRb.rotation

Later you are using 以后你用

ballRb.velocity = ballRb.velocity * ballController.bulletSpeed * Time.deltatime;

but right before you have set 但就在您设定之前

ballRb = Vector2.zero;

So the multiplication results in Vector2.zero . 因此,乘法结果为Vector2.zero Also adding Time.deltaTime (typo btw) in this one-time assignment makes no sense. 同样在这个一次性分配中添加Time.deltaTime (typo btw)是没有意义的。

Also if you remove this line you are not taking the new rotation into account when assigning a new velocity. 同样,如果删除了这条线,则在分配新速度时不会考虑新的旋转。

The velocity is in global space. velocity在全局空间中。 You also can't use eg transform.right as new direction since the transform isn't updated .. the Rigidbody2D is .. so you can use GetRelativeVector in order to set the new local direction after rotating 您也不能使用例如transform.right作为新方向,因为该变换未更新.. Rigidbody2D为..所以您可以使用GetRelativeVector以便在旋转后设置新的局部方向

private void OnTriggerEnter2D(Collider2D ball)
{
    // The assignment of the GameObject
    // was kind of redundant except
    // you need the reference for something else later

    var ballRb = ball.GetComponent<Rigidbody2D>();

    // set position through RigidBody component
    ballRb.position = this.transform.position;

    // Rotate the ball using the Rigidbody component
    // Was the int overload intented here? Returning int values 0-359
    // otherwise rather use the float overload by passing float values as parameters
    ballRb.rotation = Random.Range(0f, 360f);

    // Start moving the ball again
    // with a Vector different to Vector2.zero 
    // depending on your setup e.g.
    ballRb.velocity = ballRb.GetRelativeVector(Vector2.right * ballController.bulletSpeed);
}

little demo 小样

在此处输入图片说明

for the walls btw I used something very similar: 顺便说一句,我使用了非常相似的墙壁:

var ballRb = ball.GetComponent<Rigidbody2D>();
var newDirection = Vector2.Reflect(ballRb.velocity, transform.up);
ballRb.rotation = ballRb.rotation + Vector2.SignedAngle(ballRb.velocity, newDirection);
ballRb.velocity = newDirection.normalized * ballController.bulletSpeed;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM