Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Top-Down Shooter controller walk speed error

Discussion in 'Scripting' started by TheDiddyHop, Feb 22, 2015.

  1. TheDiddyHop

    TheDiddyHop

    Joined:
    Jul 26, 2013
    Posts:
    30
    Hi, i've been working on a top down shooter using sebastian lagues tutorial. I've been working on a game which doesn't require running, therefore i decided to remove the run functionality from the script. The script gets no errors in the console, but when i move, it moves at an incredibly slow speed... I must of made some mistake but i honestly dont have a clue what it is!

    Here's my code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent (typeof (CharacterController))]
    5. public class PlayerController : MonoBehaviour {
    6.    
    7.     // Handling
    8.     public float rotationSpeed = 450;
    9.     public float walkSpeed = 5;
    10.    
    11.     // System
    12.     private Quaternion targetRotation;
    13.    
    14.     // Components
    15.     private CharacterController controller;
    16.  
    17.     void Start () {
    18.         controller = GetComponent<CharacterController>();
    19.     }
    20.    
    21.  
    22.     void Update () {
    23.         Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical"));
    24.        
    25.        
    26.         if (input != Vector3.zero) {
    27.             targetRotation = Quaternion.LookRotation(input);
    28.             transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y,targetRotation.eulerAngles.y,rotationSpeed * Time.deltaTime);
    29.         }
    30.        
    31.         Vector3 motion = input;
    32.         motion *= (Mathf.Abs(input.x) == 1 && Mathf.Abs(input.z) == 1)?.7f:1;
    33.         motion += Vector3.up * -8;
    34.        
    35.         controller.Move(motion * Time.deltaTime);
    36.     }
    37. }
     
  2. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    Not sure if I understood fine.. but
    try changing this:
    Code (csharp):
    1.  
    2. motion *=(Mathf.Abs(input.x)==1&&Mathf.Abs(input.z)==1)?.7f:1;
    3.  
    to this:
    Code (csharp):
    1.  
    2. motion *= 50;     //replace the value to another one that fits better to you
    3.  
    To move a controller, you just need to multiply the motion value, then.. Time.deltaTime will be the one who will handle it softly...
     
  3. TheDiddyHop

    TheDiddyHop

    Joined:
    Jul 26, 2013
    Posts:
    30
    This has increased the movement speed, however when moving diagonal, the speed seems to increase... do you know how to fix this?

    Here's a video showing the problem:


    I'm not sure if it's just me, but it definitely looks dodgy in my opinion...