- Home /
Question by
DevFlako · Oct 16, 2020 at 04:23 AM ·
transform.translate
Exact Smooth Movement with transform.Translate
So I am very new to C# and I'm trying to make a smooth transition from aiming down sights to not aiming them down and vice versa, instead of it just teleporting there. Here's the code:
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using UnityEngine;
public class gunScript : MonoBehaviour
{
bool isMouse2Down;
bool isMouse2Up;
// 0, -0.522, 1.217 ADS Coords
// 0.744, -0.75899, 1.217 default coords
void Start()
{
}
// Update is called once per frame
void Update()
{
isMouse2Down = Input.GetKeyDown(KeyCode.Mouse1);
isMouse2Up = Input.GetKeyUp(KeyCode.Mouse1);
if (isMouse2Down)
{
transform.Translate(-0.744f, 0.23699f, 0f);
}
else if (isMouse2Up)
{
transform.Translate(0.744f, -0.23699f, 0f);
}
}
}
Comment
Answer by Ady_M · Oct 16, 2020 at 04:59 AM
Try using Vector3.MoveTowards()
https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html
And Vector3.Distance() to check when the desired position has been reached.
Your answer
Follow this Question
Related Questions
Midair Strafing 3 Answers
Align Parent object using child object as point of reference 3 Answers
Confusion in deciding the units for game objects 1 Answer
How do I test for the Y Coordinate? 1 Answer