- Home /
Quaternion.Lerp and Vector3.MoveTowards arent smoothing
The player can interact with a door and if it's raycasting towards the interactable object and the player left clicks/right trigger it either moves or rotates the object to or from the original position/rotation.
And it does do it, but not smooth even though I'm multiplying the Time.deltaTime part by a decimal (regardless of what I multiply it by it will instantly jump to that value).
Also as for the rotation part, it glitches out a lot (rotates immediately, but not even to the desired quaternion).
private var m_isAxisInUse = false;
var needtag : boolean = false;
var Interactable : GameObject;
var toggle : boolean = false;
var on : boolean = false;
var animating : boolean = false;
var animName : String = "";
private var animStarted : boolean = false;
var moving : boolean = false;
private var moved : boolean = false;
private var originDest : Vector3;
var moveDest : Vector3;
var moveSpeed : float = 0.1;
var rotating : boolean = false;
private var rotated : boolean = false;
private var originRot : Quaternion;
var rotDest : Quaternion;
var rotSpeed : float = 0.1;
var style : GUIStyle = null;
private var difference : int = 90;
public var Message : String = "";
public var Message2 : String = "";
private var isViewing : boolean = false;
var SoundEffect : AudioSource;
var usingMultiClips : boolean = false;
var Clip1 : AudioClip;
var Clip2 : AudioClip;
function Start(){
originDest = Interactable.transform.position;
originRot = Interactable.transform.rotation;
}
function Update () {
var hit : RaycastHit;
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (transform.position, fwd, hit, 1000)) {
if(Vector3.Distance(Interactable.transform.position, transform.position) < 4){
if(hit.transform == Interactable.transform && !needtag){
isViewing = true;
}
if(hit.transform.tag == Interactable.tag && needtag){
isViewing = true;
}
}
}
if(hit.transform.tag != Interactable.tag && needtag){
isViewing = false;
}
if(hit.transform != Interactable.transform && !needtag){
isViewing = false;
}
if(isViewing == true){
if( Input.GetAxisRaw("Interact") != 1){
if(m_isAxisInUse == false){
if(toggle){
on = !on;
if(on){
Interactable.SetActive(true);
SoundEffect.clip = Clip1;
SoundEffect.Play();
}
else if(!on){
Interactable.SetActive(false);
SoundEffect.clip = Clip2;
SoundEffect.Play();
}
if(!usingMultiClips){
SoundEffect.Play();
}
}
if(animating){
if(!animStarted){
GetComponent.<Animation>().Play(animName);
animStarted = true;
SoundEffect.Play();
}
}
if(moving){
moved = !moved;
if(!moved){
StartCoroutine("Move1");
Debug.Log("Should be moving");
if(usingMultiClips){
SoundEffect.clip = Clip1;
SoundEffect.Play();
}
}
else if(moved){
StartCoroutine("Move2");
Debug.Log("Should be moving");
if(usingMultiClips){
SoundEffect.clip = Clip2;
SoundEffect.Play();
}
}
if(!usingMultiClips){
SoundEffect.Play();
}
}
if(rotating){
rotated = !rotated;
if(!rotated){
//Interactable.transform.rotation = Quaternion.Lerp(originRot, rotDest, Time.deltaTime * rotSpeed);
if(usingMultiClips){
SoundEffect.clip = Clip1;
SoundEffect.Play();
}
}
else if(rotated){
//Interactable.transform.rotation = Quaternion.Lerp(rotDest, originRot, Time.deltaTime * rotSpeed);
if(usingMultiClips){
SoundEffect.clip = Clip2;
SoundEffect.Play();
}
}
if(!usingMultiClips){
SoundEffect.Play();
}
}
// Call your event function here.
m_isAxisInUse = true;
}
}
if( Input.GetAxisRaw("Interact") == 1){
m_isAxisInUse = false;
}
}
}
function Move1(){
var step = moveSpeed * Time.deltaTime;
Interactable.transform.position = Vector3.MoveTowards(originDest, moveDest, step);
}
function Move2(){
var step2 = moveSpeed * Time.deltaTime;
Interactable.transform.position = Vector3.MoveTowards(moveDest, originDest, step2);
}
function OnGUI () {
if(isViewing == true){
if(!toggle && animating){
GUI.Label( Rect( (Screen.width/2)-difference, 10, 200, 25 ), Message, style);
}
else if(toggle){
if(!on){
GUI.Label( Rect( (Screen.width/2)-difference, 10, 200, 25 ), Message, style);
}
else if(on){
GUI.Label( Rect( (Screen.width/2)-difference, 10, 200, 25 ), Message2, style);
}
}
if(moving){
if(!moved){
GUI.Label( Rect( (Screen.width/2)-difference, 10, 200, 25 ), Message, style);
}
else if(moved){
GUI.Label( Rect( (Screen.width/2)-difference, 10, 200, 25 ), Message2, style);
}
}
if(rotating){
if(!rotated){
GUI.Label( Rect( (Screen.width/2)-difference, 10, 200, 25 ), Message, style);
}
else if(rotated){
GUI.Label( Rect( (Screen.width/2)-difference, 10, 200, 25 ), Message2, style);
}
}
}
if(isViewing == false || animStarted == false){
GUI.Label( Rect( (Screen.width/2)-difference, 10, 200, 25 ), "", style);
}
}
Please help me by pointing me in the direction of how I would be able to fix this. Thanks for reading, and happy holidays!
Answer by Fattie · Dec 22, 2015 at 10:28 PM
Among other problems you don't seem to be setting originDest
, in the code presented. Setting it "in Start" would not work.
In Update
you should ONLY catch the trigger of the movement.
Then add a line of code
Debug.Log("Door should rotate");
or similarly for the drawer.
Then write a SEPARATE ROUTINE to do the animation. You'd likely call that routine with a StartCoroutine
Once you do that and can show your output here, edit the question and someone will be sure to help.
Sorry about that, I just condensed the script so nobody would get confused with the extra functionality in it, but I found through debugging that the move coroutine is only called once when clicked on, but it still no matter what the value of movespeed is just teleports to that position ins$$anonymous$$d of lerping to it.
no need to be sorry but be sure to click edit and ... actually I tell you what. I will delete this question since it is old now. Please go ahead and post a new quetsion with your totally new code
Cheers!
Follow this Question
Related Questions
[SOLVED] Rotation/angled offset in Quaternion.Lerp for a carried GameObject 2 Answers
Vector3.MoveTowards and Quaternion.RotateTowards 1 Answer
Quaternion.Lerp and Vector3.MoveTowards arent smoothing 1 Answer
Smooth camera 90 degree rotation around target 1 Answer
Changing rotation of Quaternion 0 Answers