简体   繁体   English

我如何左右移动气缸?

[英]How can i move a cylinder right and left?

Between two points or only to the left nonestop or only to the right nonestop. 在两点之间,或仅在左边不停或仅在右边不停。

In this code i spin the cylinder but i can't move it to the sides: 在此代码中,我旋转圆柱体,但无法将其移到侧面:

using UnityEngine;
using System.Collections;

public class MakeTwoPoints3D : MonoBehaviour
{
    public float speed = 10f;

    public float delta = 15.5f;  // Amount to move left and right from the start point
    public float moveSpeed = 5.0f;
    private Vector3 startPos;

    void Start()
    {
        startPos = transform.position;
    }

    void Update()
    {
        transform.Rotate(Vector3.up, speed * Time.deltaTime);
        transform.position += transform.right * Time.deltaTime * moveSpeed;
    }
}

If i make transform.right it will move the cylinder in circle on place up and down in circle. 如果我进行transform.right,它将使圆柱体在圆周上上下移动。 If i make transform.up it will move it to me i mean like forward but to the camera but at least it will move it. 如果我进行transform.up,它将把它移动给我,我的意思是像向前一样但移到相机,但至少它将移动它。 And if i make transform.Forward again it will make circles and will remove the cylinder in circles up down. 如果我进行了transform.Forward,它将再次绕圈,并将圆柱体向上向下取下。

I can't figure out how to move it to the sides. 我不知道如何将其移到侧面。

You need use Vector3.right instead of transform.right . 您需要使用Vector3.right而不是transform.right

void Update()
{
    transform.Rotate(Vector3.up, speed * Time.deltaTime);
    transform.position += Vector3.right * Time.deltaTime * moveSpeed;
}

When you use transform.right , the Vector3 will adopt the local rotations of that object's transform. 当您使用transform.right ,Vector3将采用该对象的变换的局部旋转。 Meaning, if the object was rotated 45 degrees around the Y axis, your transform.right vector would be on an angle. 意思是,如果对象绕Y轴旋转了45度,则您的transform.right向量将成一个角度。 If you keep translating an object along it's local axis while you rotate it, it will travel in a circle. 如果在旋转对象时始终沿其局部轴平移对象,则该对象将绕圆周运动。

On the other hand, Vector3.right is always in world space so it will always face "true" right. 另一方面, Vector3.right始终在世界空间中,因此它将始终面对“真实”的权利。

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

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