- Home /
Smooth Moving GameObject
I can't seem to apply a Vector.Lerp because the var buildingObject is a GameObject and not Transform. how can I convert it into a Transform purely for the purpose of making it smooth move, without actually changing the var type.
var buildingObject : GameObject; var myGround : Transform; var smooth = 5.0;
function Update () {
checkForBuilding();
}
function checkForBuilding() {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100.00) && hit.collider.gameObject.CompareTag("CursorSnap")){
buildingObject.transform.position = hit.point;
}
}
Using
buildingObject.transform.position = Vector3.Lerp (hit.point, Transform.position,Time.deltaTime * smooth);
returns the error BCE0020: An instance of type 'UnityEngine.Transform' is required to access non static member 'position'. maybe i've interpreted it wrong.
Answer by Alec-Slayden · Feb 24, 2011 at 07:21 PM
It shouldn't matter that the variable is a game object, since game objects inherently have transforms. Just make sure what you're using the Vector3.Lerp on is buildingObject.transform.position or .rotation
Make sure in your lerp that you use
transform.position
NOT
Transform.position
Transform with a capital 'T' is a type, like integer or float. The compiler wants to know what transform to use.
transform with a lowercase 't' refers to whatever transform this script is attached to.
In addition, it looks like you're trying to move the building smoothly to the hit.point. if thats the case, you may want to use this instead:
buildingObject.transform.position = Vector3.Lerp(buildingObject.transform.position, hit.point,Time.deltaTime * smooth);
Lerp has the parameters (from, to, speed/time), so you'll want to use its position, not the script transform's position (assuming you're not running this script from that building object).
The effect this has will be a decelerating Lerp. If you want a smooth A to B without slowing down much, consider SmoothDamp instead :)
using 'buildingObject.transform.position = Vector3.Lerp (hit.point, Transform.position,Time.deltaTime * smooth);' returns the error 'BCE0020: An instance of type 'UnityEngine.Transform' is required to access non static member 'position'.' maybe i've interpreted it wrong.
I expanded a little bit, I think I have a better picture of what you're trying to do, hopefully this clears things up :)
Thats perfect, that was so frustrating how close i was :/ thanks, but after all of that that didn't solve my problem of "jerky" movements :(
Your answer
Follow this Question
Related Questions
How Can I Make The Parkour Moves Move Smoothly Instead Of Instantly Teleport? 1 Answer
How to smooth movement object in one-axis? 1 Answer
Transform.position can't be changed smoothly. 2 Answers
make player move in direction it's facing 2 Answers
What is the reasoning of multiplying by Time.deltaTime 1 Answer