- Home /
Question by
el-mas-pro-470 · Dec 08, 2018 at 09:01 PM ·
smoothaimlocalpositionsmoothingsmoothly
position change smoothed?
Hi, how do I change the position of localposition to be smoothed? This looks more realistic (Weapon Aim Script). Thank you! (slerp do not work). script:
using UnityEngine;
using System.Collections;
public class AimDownSights : MonoBehaviour {
public Vector3 AimDownSight;
public Vector3 HipFire;
public float AimSpeed = 5;
void Update ()
{
if(Input.GetMouseButtonDown(1))
{
transform.localPosition = Vector3.Slerp(transform.localPosition,AimDownSight, AimSpeed * Time.deltaTime);
}
if(Input.GetMouseButtonUp(1))
{
transform.localPosition = HipFire;
}
}
}
Comment
Best Answer
Answer by leftshoe18 · Dec 08, 2018 at 09:11 PM
Well Slerp would work but not in the way you implemented it. You're only taking a single step on the frame the button gets pressed.
using UnityEngine;
using System.Collections;
public class AimDownSights : MonoBehaviour {
public Vector3 AimDownSight;
public Vector3 HipFire;
public float AimSpeed = 5;
// add a reference to what you want the target position to be //
Vector3 targetPosition;
void Update ()
{
transform.localPosition = Vector3.Slerp(transform.localPosition, targetPosition, AimSpeed * Time.deltaTime);
if(Input.GetMouseButtonDown(1))
{
targetPosition = AimDownSight;
}
if(Input.GetMouseButtonUp(1))
{
targetPosition = HipFire;
}
}
}
This will make the mouse button change whether you want to move towards the HipFire or AimDownSight positions as the targetPosition. The slerp will run every frame to move you towards the targetPosition.
Edit: formatting