When you move with the Character controller, even if you point the character in the direction of movement, it will return immediately.

Asked 1 years ago, Updated 1 years ago, 80 views

When I move characters, I use LookRotation() to specify the direction of the characters, but when I stop, the direction of the characters is reversed.Please let me know if you have any solutions or ideas.

using System.Collections;
using System.Collections.General;
using UnityEngine;

public class CharacterMove:MonoBehavior {

    public float walkSpeed; // walking speed
    public float runSpeed;//Running speed

    private Vector3 moveDirection=Vector3.zero;// Amount to be moved
    private Vector 3 direction; // direction to move
    private float x;//horizontal
    private float z;// vertical
    private float gravity=98f;// Downward movement
    private CharacterController controller; // character controller



    // Use this for initialization
    void Start() {
        // character controller retrieval
        controller=GetComponent<CharacterController>();;
    }

    // Update is called once per frame
    void Update() {

        // Define GetAxis
        x = Input.GetAxis("Horizontal");
        z = Input.GetAxis("Vertical");

        direction = new Vector 3(x,0,z); // GetAxis to get directions

        if(Input.GetMouseButton(1)){// Right-click and move to dash
            moveDirection=direction*runSpeed;
        } else {
            moveDirection=direction*walkSpeed;
        }


        // fall down if not on the ground
        if(controller.isGrounded){
            moveDirection.y = 0f;
        } else {
            moveDirection.y - =gravity*Time.deltaTime;
        }

        // move
        controller.Move(moveDirection*Time.deltaTime);

        // This seems to be the problem.While moving, it rotates, but when it is finished, it returns to its original orientation.
        Quaternion q=Quaternion.LookRotation(direction);
        transform.rotation=Quaternion.RotateTowards(transform.rotation,q,1200.0f*Time.deltaTime);


    }
}
  • Try changing the Lookrotation part

    Vector3 relativePos=target.position-transform.position;
    Quaternion rotation=Quaternion.LookRotation(relativePos);
    transform.rotation=rotation;
    

    It didn't work.

  • Try putting moveDirection in the argument

    Quaternion q=Quaternion.LookRotation(moveDirection);
    

    It didn't work.

  • Return when x,z is not entered

     if(x==0&z==0){
            return;
        } else {
            direction = new Vector 3(x,0,z);
        }
    

    I was wondering if I could go, but if I returned, it would not be processed after that, so if I stopped in the air, it would not fall.

Try changing the Lookrotation part

Vector3 relativePos=target.position-transform.position;
Quaternion rotation=Quaternion.LookRotation(relativePos);
transform.rotation=rotation;

It didn't work.

Try moveDirection as an argument

Quaternion q=Quaternion.LookRotation(moveDirection);

It didn't work.

Return when x,z is not entered

 if(x==0&z==0){
        return;
    } else {
        direction = new Vector 3(x,0,z);
    }

I was wondering if I could go, but if I returned, it wouldn't be processed after that, so if I stopped in the air, it wouldn't fall.

c# unity3d

2022-09-30 21:34

1 Answers

If I set it to return, the processing will stop, so I chose the if statement.

 if(x!=0||z!=0){
        direction = new Vector 3(x,0,z);
    }

This led to the "Update direction when x,z is not 0 (entered)", and even if you stop, the direction does not return to its original position, and landing decisions continue to be processed and dropped.


2022-09-30 21:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.