简体   繁体   中英

How to rotate object slowly in this situation in unity?

I am creating a building system in unity and when a key is pressed this function is run. In this function I want to rotate the buildModel 90 degrees slowly over time .

Here is the function and what I tried:

public void RotateBuild()
{

    //buildModel.transform.Rotate(0, 0, 90.0f);
    Vector3 newDirection = Vector3.RotateTowards(buildModel.transform.eulerAngles, buildModel.transform.localEulerAngles + new Vector3(0, 90, 0), Time.fixedDeltaTime, 0);
    buildModel.transform.rotation = Quaternion.LookRotation(newDirection);
}

I want to rotate it on the y axis, just like in Fortnite. Unfortunately, it doesn't work now, so does anyone know a solution?

private float _rotationSpeed = 180f;
private Quaternion _targetRotation;

void Start(){
    _targetRotation = buildModel.transform.rotation;
}

public void RotateBuilding(){
    _targetRotation = buildModel.transform.rotation * Quaternion.AngleAxis(90f, Vector3.up);
}

public void Update(){
    buildModel.transform.rotation = Quaternion.RotateTowards(buildModel.transform.rotation, _targetRotation, Time.deltaTime * _rotationSpeed);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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