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

Trying to get this movment style

Discussion in 'Scripting' started by besieger1, Jun 21, 2015.

  1. besieger1

    besieger1

    Joined:
    Jul 26, 2012
    Posts:
    34
    How can I get this type of movement?

    The player always moves forward even when turning, the player would move forward into the turn even if a turn key was pressed.

    E.g I want to move left, my player move forward and turn left until the desired position or rotation is achieved.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Assuming you mean like the beginning of the video, when he's going in circles, and not later on when he's doing the twin-stick style movement: Just move them by their forward transform and change their rotation as appropriate (button is down).

    Code (csharp):
    1.  
    2. transform.position += transform.forward * speed * Time.deltaTime;
    3.  
     
  3. besieger1

    besieger1

    Joined:
    Jul 26, 2012
    Posts:
    34
    Thanks for your reply but while that would work with button down is there a way to make this work while using imputs from the unity input manager?

    Code (csharp):
    1.  
    2. Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical"));
    3.  
    4.  if (input != Vector3.zero) {
    5.  targetRotation = Quaternion.LookRotation(input);
    6.  transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y,targetRotation.eulerAngles.y,rotationSpeed * Time.deltaTime);
    7.  }
    8.  
    9.  currentVelocityMod = Vector3.MoveTowards(currentVelocityMod,input,acceleration * Time.deltaTime);
    10.  Vector3 motion = currentVelocityMod;
    11.  motion *= (Mathf.Abs(input.x) == 1 && Mathf.Abs(input.z) == 1)?.7f:1;
    12.  motion *= (Input.GetButton("Run"))?runSpeed:walkSpeed;
    13.  motion += Vector3.up * -8;
    14.  
     
  4. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Actually, its best if you use a Rigidbody and change the velocity. If you manually change the position, it can ignore collisions and go right through walls. With a rigidbody, however, if it hits something it will stop or bump if you have a physics material on it.

    Code (CSharp):
    1. Rigidbody m_rigidbody;
    2.  
    3. this.m_rigidbody.velocity += transform.forward * speed * Time.deltaTime;