- Home /
Attempting to make sphere travel in direction of camera
As the title states, I'm trying to make a sphere travel in the direction of the camera upon pressing 'E'. Here's its script at the moment.
#pragma strict
var used = false;
var time = 0;
var direction = new Vector3(0, 0, 0);
var target : Transform;
function Start () {
used = false;
time = 0;
direction = Camera.main.transform.forward;
}
function Update () {
if(!used){
direction = Camera.main.transform.forward;
transform.position = target.position;
if(Input.GetKeyDown(KeyCode.E)){
used = true;
}
}
else{
transform.position = transform.position + direction * 10 * Time.timeDelta;
time++;
}
}
There at least two problems with this that I've noticed so far, though. The first is that, upon pressing 'E', the sphere does not travel in the direction of 'direction', but rather simply goes straight to the left. The second problem is that 'direction' changes as the direction of the camera changes, as it should, but it does so before and after pressing 'E', even though it should only before pressing 'E'. The object has no gravity and its sphere collider is not a trigger. Any chance you can help me modify the script so it works how I want?
EDIT: O...k... for some reason, even if I COMPLETELY DELETE THE SCRIPT, the object this script is on just teleports straight to the camera and I cannot figure out why.
Answer by Eric5h5 · Dec 31, 2014 at 09:30 PM
You need to read the error messages in Unity, and fix them, before scripts can be used. In this case there is no "timeDelta" property of Time. (Not relevant, but your "time" variable is misnamed since it's counting frames, not time.)
Your answer
![](https://koobas.hobune.stream/wayback/20220613175313im_/https://answers.unity.com/themes/thub/images/avi.jpg)