- Home /
Make Quaternion affected by float
Basically, I'm just trying to make targetRotation become affected by the float influence (a float ranging from 0 to 1). So if influence is 0, targetRotation will be 0. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class RotationConstraint : MonoBehaviour { [SerializeField] private Transform targetObject;
[SerializeField]
[Range(0, 1)]
private float influence;
private Quaternion targetRotation;
void Update ()
{
targetRotation = targetObject.transform.rotation;
transform.rotation = targetRotation * influence; // I get an error under targetRotation * influence
}
}
Answer by Bunny83 · Oct 28, 2018 at 10:21 AM
"targetRotation will be 0" just makes no sense. What does 0 mean? A rotation / orientation is never "0". Do you mean the identity rotation? So eulerangles 0,0,0? In this case you can use Slerp. However if you just want to rotate towards you can use RotateTowards.
To use Slerp between the identity rotation and your target rotation you would do something like this:
transform.rotation = Quaternion.Slerp(Quaternion.identity, targetRotation, influence);
In this case if influence is 0 the rotation will be the identity rotation. If it's 1 the rotation will be equal to the targetrotation. Of course if you want to slerp between two orientations you just have to pass the starting rotation as first parameter. However keep in mind that the start rotation has to be constant if you want that 0 is the start rotation. So using the current rotation as start rotation would be a decelerated "rotate towards" if influence is greater than 0.
Thanks! So I am trying to use influence to decide how much my rotation is affected by the other object during the game. So if the object rotated 360 degrees and the influence is set to 0.5, I should only rotate half the amount (so 180 degrees), and if influence was set to 1, I would be rotated 360 degrees just like the object. So I am having trouble trying to decide what to use as the start rotation in the slerp.
Your answer
Follow this Question
Related Questions
Prevent child rotation but still control it yourself 1 Answer
How to use quaternions to apply an offset to a rotation to calibrate a controller 1 Answer
Get real compass direction 1 Answer
How do i make my Mouselook behave when changing the z rotation? 0 Answers
How can I instantiate a gameobject facing another gameobject 2D? 0 Answers