Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Running in a circle, far views and performance drops

Discussion in 'Scripting' started by Kashrlyyk, Jul 18, 2012.

  1. Kashrlyyk

    Kashrlyyk

    Joined:
    Aug 16, 2011
    Posts:
    17
    I use this script to control the character:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SimpleControls : MonoBehaviour {public float speed = 16.0F;
    5.     public float jumpSpeed = 18.0F;
    6.     public float gravity = 20.0F;
    7.     private Vector3 moveDirection = Vector3.zero;
    8.     private Vector2 rotationSpeed = new Vector2 (75, 50);
    9.     private float currentRotation = 75f;
    10.     private float rotationLimitX = 150f;
    11.    
    12.    
    13.     void Update() {
    14.         CharacterController controller = GetComponent<CharacterController>();
    15.         if (controller.isGrounded) {
    16.             if (Mathf.Abs (Input.GetAxis ("Horizontal")) < 0.1f)
    17.                 currentRotation = rotationSpeed.x;
    18.             else
    19.                 currentRotation += 1f;
    20.             if (currentRotation > rotationLimitX) currentRotation = rotationLimitX;
    21.            
    22.             moveDirection = new Vector3(Input.GetAxis("Strafe"), 0, Input.GetAxis("Vertical"));
    23.             moveDirection = transform.TransformDirection(moveDirection);
    24.             moveDirection *= speed;
    25.            
    26.             transform.Rotate(0, currentRotation * Time.deltaTime * Input.GetAxis ("Horizontal"), 0);
    27.             if (Input.GetButton("Jump"))
    28.                 moveDirection.y = jumpSpeed;
    29.            
    30.         }
    31.         moveDirection.y -= gravity * Time.deltaTime;
    32.         controller.Move(moveDirection * Time.deltaTime);
    33.     }
    34.         public Transform cameraPivot;
    35. }
    If I stand still and rotate the character everything is fine. But if I run in a circle the rotation is only smooth if something is blocking the view like a hill or so. When the view is far the rotation visibly jumps when running. Right now I only have one terrain, not trees or anything else.

    So how can I fix that performance problem.
     
  2. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    938
    GetComponent shouldn't be called every frame. this is a very slow function. Store it in a variable of type CharacterController, name it something like controller, use the GetComponent in the start or awake function to set the reference and use controller in the rest of your script. See if that helps
     
  3. Kashrlyyk

    Kashrlyyk

    Joined:
    Aug 16, 2011
    Posts:
    17
    Thank you for the answer, but it didn't help. I also cached the "transform" in the code. Still not better.
     
  4. Kashrlyyk

    Kashrlyyk

    Joined:
    Aug 16, 2011
    Posts:
    17
    I've added something I've found on the internet. It lets me rotate the view with the right mouse button.

    Code (csharp):
    1.  
    2. //Mouse View from here!!!
    3.         if (Input.GetMouseButtonDown(1)) {
    4.             changingView = true;
    5.             initialPosition = Input.mousePosition;
    6.         }
    7.         if (Input.GetMouseButtonUp(1))
    8.             changingView = false;
    9.         if (changingView) {
    10.             currentPosition.x = Mathf.Clamp ((Input.mousePosition.x - initialPosition.x) / 100, -3, 3) *rotationSpeed.x *Time.deltaTime;
    11.             currentPosition.y = Mathf.Clamp ((Input.mousePosition.y - initialPosition.y) / 100, -2, 2) *rotationSpeed.y *Time.deltaTime;
    12.             myTransform.Rotate (0, currentPosition.x, 0, Space.World);
    13.             cameraPivot.Rotate (-currentPosition.y, 0, 0);
    14.         }
    15.         if (Mathf.Abs( Input.GetAxis("Vertical")) > 0.25f )
    16.             cameraPivot.rotation = Quaternion.Lerp (cameraPivot.rotation, transform.rotation, Time.deltaTime);
    17.  
    No stuttering or lagging when running in a circle with this. Why? Is it possible to change the code in the first post to be more like the one in this post?
     
  5. Kashrlyyk

    Kashrlyyk

    Joined:
    Aug 16, 2011
    Posts:
    17
    I have the same problem with the 3rd Person Character Controller that comes with Unity in a new project with a different terrain. The slow rotation happened whenever part of the terrain were culled because it got to close to the camera.

    My project uses a first person perspective. So all the hills behind me are removed and that slows the rotation down??? Could that be?

    EDIT: Nope. That still doesn't explain the mouse rotation without stutter.
     
    Last edited: Aug 9, 2012
  6. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    938
    Well you could try different "culling sizes". So try using smaller tiles you are culling, this will reduce the loading time for every tile, but you will be more frequently tile-swapping
     
  7. timsk

    timsk

    Joined:
    May 27, 2011
    Posts:
    177
    Put a kinematic rigibody on your character and move your code to FixedUpdate. See if that helps.
     
  8. Kashrlyyk

    Kashrlyyk

    Joined:
    Aug 16, 2011
    Posts:
    17
    I've added a rigidbody to the player, checked "isKinematic" and changed "Update" to "FixedUpdate" in the controls script: No changes. So either I did something wrong or it doesn't help.

    I assume you are talking about "occlusion culling". I don't use the Pro version.

    Besides my current scene is nearly empty. 4 enemies( actually just some cubes and spheres etc. put together with some scripts) , 3 chests, 1 column with light, 1 sun and 1 moon and 1 terrain. That's all that is rendered.
     
    Last edited: Aug 27, 2012
  9. BPPHarv

    BPPHarv

    Joined:
    Jun 9, 2012
    Posts:
    318
    This might help , I've run into stutter when I try to modify the camera in an Update() function. Do your model reposition calc / updates in the update function as you do now but move any functions that modify the camera or it's transform into the LateUpdate() function. You can easily check to see if this is your issue by disabling any of your camera script (temporarily by commenting out lines that deal with anything camera related) and using one of the stock camera scripts (SmothFollowTarget or MouseOrbit) to verify if this is indeed the issue.

    From the unity docs

     
  10. Kashrlyyk

    Kashrlyyk

    Joined:
    Aug 16, 2011
    Posts:
    17
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SimpleControls : MonoBehaviour {
    5.     public float speed = 16.0F;
    6.     public float runModifier = 2.0f;
    7.     public float jumpSpeed = 18.0F;
    8.     public float gravity = 20.0F;
    9.     private Vector3 moveDirection = Vector3.zero;
    10.     private Vector2 rotationSpeed = new Vector2 (25, 20);
    11.     private float currentRotation = 225f;
    12.     //private float rotationLimitX = 150f;
    13.     private bool changingView = false;
    14.     private Vector3 initialPosition;
    15.     private Vector3 currentPosition = Vector3.zero;
    16.     CharacterController controller;
    17.     private Transform myTransform;
    18.    
    19.    
    20.     void Awake() {
    21.         controller = GetComponent<CharacterController>();  
    22.         myTransform = transform;
    23.     }
    24.    
    25.     void Update() {
    26.        
    27.         if (controller.isGrounded) {
    28.             if (Mathf.Abs (Input.GetAxis ("Horizontal")) > 0)
    29.                 myTransform.Rotate(0, currentRotation * Time.deltaTime * Input.GetAxis ("Horizontal"), 0);
    30.                
    31.             if (Mathf.Abs (Input.GetAxis ("Vertical")) > 0 || Mathf.Abs (Input.GetAxis ("Strafe")) > 0) {
    32.                 //Debug.Log(Input.GetAxis("Vertical"));
    33.             moveDirection = new Vector3(Input.GetAxis("Strafe") * speed *3/4, 0, Input.GetAxis("Vertical") * speed);
    34.             moveDirection = myTransform.TransformDirection(moveDirection);
    35.             }
    36.             if(Input.GetKey("left shift")) moveDirection *= runModifier;
    37.             if (Input.GetButton("Jump"))
    38.                 moveDirection.y = jumpSpeed;  
    39.            
    40.             if (Mathf.Abs(Input.GetAxis ("Vertical")) < 0.05  Mathf.Abs (Input.GetAxis("Strafe")) < 0.05) {
    41.             moveDirection = Vector3.zero;  
    42.             }
    43.        }
    44.        
    45.        
    46.         moveDirection.y -= gravity * Time.deltaTime;
    47.         //controller.move command was here
    48.        
    49.         //Mouse View from here!!!
    50.         if (Input.GetMouseButtonDown(1)) {
    51.             changingView = true;
    52.             initialPosition = Input.mousePosition;
    53.         }
    54.         if (Input.GetMouseButtonUp(1)) {
    55.             changingView = false;
    56.         }
    57.         if (changingView) {
    58.             currentPosition.x = Mathf.Clamp ((Input.mousePosition.x - initialPosition.x) / 100, -3, 3) *rotationSpeed.x *Time.deltaTime;
    59.             currentPosition.y = Mathf.Clamp ((Input.mousePosition.y - initialPosition.y) / 100, -2, 2) *rotationSpeed.y *Time.deltaTime;
    60.             //myTransform.Rotate was here followed by cameraPivot.Rotate
    61.                        
    62.         }
    63.        
    64.         //cameraPivot.rotation was here
    65.     }
    66.         public Transform cameraPivot;
    67.        
    68.     void LateUpdate() {
    69.     controller.Move(moveDirection * Time.deltaTime);
    70.     if (changingView) {
    71.             myTransform.Rotate (0, currentPosition.x, 0, Space.World);
    72.             cameraPivot.Rotate (-currentPosition.y, 0, 0);
    73.             }
    74.            
    75.     if (Mathf.Abs( Input.GetAxis("Vertical")) > 0.25f )
    76.             cameraPivot.rotation = Quaternion.Lerp (cameraPivot.rotation, transform.rotation, Time.deltaTime);
    77.     }
    78.     /*public CharacterController character;
    79.     private Vector3 initialPosition;
    80.     private Vector3 currentPosition = Vector3.zero;
    81.     private float speed = 10f;
    82.    
    83.     private bool changingView = false;
    84.  
    85.     void Update ()
    86.     {
    87.         if (Mathf.Abs (Input.GetAxis ("Horizontal")) < 0.1f)
    88.             currentRotation = rotationSpeed.x;
    89.         else
    90.             currentRotation += 1f;
    91.         if (currentRotation > rotationLimitX) currentRotation = rotationLimitX;
    92.         character.SimpleMove(character.transform.forward * speed * Input.GetAxis("Vertical"));
    93.         character.SimpleMove(character.transform.right * speed * Input.GetAxis("Strafe"));
    94.  
    95.         transform.Rotate(0, currentRotation * Time.deltaTime * Input.GetAxis ("Horizontal"), 0, Space.World);
    96.         if (Input.GetMouseButtonDown(0)) {
    97.             changingView = true;
    98.             initialPosition = Input.mousePosition;
    99.         }
    100.         if (Input.GetMouseButtonUp(0))
    101.             changingView = false;
    102.         if (changingView) {
    103.             currentPosition.x = Mathf.Clamp ((Input.mousePosition.x - initialPosition.x) / 100, -3, 3);
    104.             currentPosition.y = Mathf.Clamp ((Input.mousePosition.y - initialPosition.y) / 100, -2, 2);
    105.             currentPosition.x *= rotationSpeed.x;
    106.             currentPosition.y *= rotationSpeed.y;
    107.             currentPosition *= Time.deltaTime;
    108.             transform.Rotate (0, currentPosition.x, 0, Space.World);
    109.             cameraPivot.Rotate (-currentPosition.y, 0, 0);
    110.         }
    111.         if (Mathf.Abs( Input.GetAxis("Vertical")) > 0.25f )
    112.             cameraPivot.rotation = Quaternion.Slerp (cameraPivot.rotation, transform.rotation, Time.deltaTime);
    113.     }*/
    114. }
    115.  
    Still stutters like hell and still no stutters when using the mouse to rotate the camera.