Question by
jamiecorkhill5 · Feb 12, 2018 at 04:50 PM ·
transformtransform.positionvectorefficiency
Is this code the most efficient possible, or are there better ways to rewrite it?
I am wondering if this code is the most efficient possible, or whether it would be laggy due to framerate or VSync. I ask because the variable "translateVector" is being recalculated and reassigned on every frame, and that does not feel like the most efficient way to make the gameobject move. Do you know of any better methods?
For information, I am trying to get my gameobject to move on a 30 degree vector up and to the right on every even click, and on a 30 degree vector up and to the left on every odd click.
Thanks,
Jamie
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Translate : MonoBehaviour {
public float speed;
public float angle;
public int counter = 0;
private Vector3 translateVector;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
counter++;
}
if(counter % 2 == 0)
{
translateVector = Quaternion.Euler(0, 0, angle) * Vector3.right * Time.deltaTime * speed;
}
else
{
translateVector = Quaternion.Euler(0, 0, -angle) * Vector3.left * Time.deltaTime * speed;
}
transform.position += translateVector;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612152500im_/https://answers.unity.com/themes/thub/images/avi.jpg)