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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Speed Control with movement script.

Discussion in 'Scripting' started by XavierM1, Jun 16, 2016.

  1. XavierM1

    XavierM1

    Joined:
    May 31, 2016
    Posts:
    2
    Hello, i stumbled upon this movement script that works with a touchscreen joystick, but i need help integrating the speed control because is normally moves fairly slow, that variable is there i just don't know how to make it effect the movement. I would greatly appreciate any help, this has had me stumped for a while. :(
    Code (CSharp):
    1. using UnityEngine;
    2. using CnControls;
    3.  
    4.  
    5. [RequireComponent(typeof(CharacterController))]
    6. public class JoystickController : MonoBehaviour
    7. {
    8.     public float MovementSpeed = 50f;
    9.  
    10.     private Transform _mainCameraTransform;
    11.     private Transform _transform;
    12.     private CharacterController _characterController;
    13.  
    14.     private void OnEnable()
    15.     {
    16.         _mainCameraTransform = Camera.main.GetComponent<Transform>();
    17.         _characterController = GetComponent<CharacterController>();
    18.         _transform = GetComponent<Transform>();
    19.     }
    20.  
    21.     public void Update()
    22.     {
    23.         // Just use CnInputManager. instead of Input. and you're good to go
    24.         var inputVector = new Vector3(CnInputManager.GetAxis("Horizontal"), CnInputManager.GetAxis("Vertical"));
    25.         Vector3 movementVector = Vector3.zero;
    26.  
    27.         // If we have some input
    28.         if (inputVector.sqrMagnitude > 0.1f)
    29.         {
    30.             movementVector = _mainCameraTransform.TransformDirection(inputVector);
    31.             movementVector.y = 0f;
    32.             movementVector.Normalize();
    33.             _transform.forward = movementVector;
    34.         }
    35.  
    36.         movementVector += Physics.gravity;
    37.         _characterController.Move(movementVector * Time.deltaTime);
    38.     }
    39. }
    40.  
     
  2. jaasso

    jaasso

    Joined:
    Jun 19, 2013
    Posts:
    64
    on line 37 try

    Code (CSharp):
    1. _characterController.Move(movementVector * MovementSpeed * Time.deltaTime);
     
    XavierM1 likes this.
  3. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    Line 28 should also probably be changed to:
    Code (CSharp):
    1. if (inputVector.sqrMagnitude > 0.0f)