- Home /
How could I make this script run a bit smoother (iOS)? Any Ideas?
I apologize to ask so blatantly, but the script works perfectly, but it just seems to be a bit laggy. Its a control script where you can only drag horizontally to look, and press two fingers to move forward.
So it works exactly like I want it to, but when you drag to look around, it does seem to react a bit slow. Is there a flaw in the script? Any ideas on how to fix this? Or is my script just really inefficient.
Any help would be great, thanks.
#pragma strict
var speed : float = 6.0;
// var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
private var h : float;
private var v : float;
var horozontalSpeed : float = .5;
var verticalSpeed : float = 0.0;
function Update()
{
if (Input.touchCount > 0){
var touch : Touch = Input.GetTouch(0);
if (Input.touchCount == 1){
horozontalSpeed = 1.56;
}
if (touch.phase == TouchPhase.Moved)
{
h = horozontalSpeed * touch.deltaPosition.x ;
transform.Rotate( 0, -h, 0, Space.World );
// v = verticalSpeed * touch.deltaPosition.y ;
// transform.Rotate( v, 0, 0, Space.World );
}
}
if (Input.touchCount > 1){
horozontalSpeed = 0.926;
//speed = 1.0;
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Fire1"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
}
Answer by Muuskii · Aug 15, 2013 at 05:16 AM
You're calling getcomponent in update() try caching the controller and transform.
It says, getcomponent can only be called from the main thread. Unless I'm not getting what your saying, I dont think that worked
Are you calling it in Start() or in the "preconstructor"? (where you define it in the declaration)
Your answer
![](https://koobas.hobune.stream/wayback/20220613112243im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Move Script Error 0 Answers
rotation snap, or any kinda snap 3 Answers
Rotate to facing direction 2.5D 2 Answers
Is there a way to detect which way an object is facing? 1 Answer
Camera move belong a certain path 1 Answer