Home > Enterprise >  How to rotate object slowly in this situation in unity?
How to rotate object slowly in this situation in unity?

Time:07-09

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?

CodePudding user response:

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);
}
  • Related