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

Rotate on Click To Move Controller

Discussion in 'Scripting' started by Cooper37, Apr 21, 2017.

  1. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    Hi all! I'm working on a game where you click to move the player. There's so many ways to do this, but I decided to use the Move method and character controller to prevent my character from going through walls(and for the sake of detecting walls). Well, I think it is because the nature of the Move that I found it difficult myself to rotate the player.

    I'm trying to make the player rotate to the direction it's moving whenever the mouse button is clicked. Does anyone know how I can do this? Would I have to use another move method and detect collision through code?

    Control.cs
    Code (csharp):
    1.  
    2. void Start(){
    3.         controller = GetComponent<CharacterController> ();
    4.     }
    5.     void FixedUpdate () {
    6.         UpdateMovement ();
    7.     }
    8.     void UpdateMovement(){
    9.         RaycastHit hit = new RaycastHit ();
    10.         if (Input.GetButtonDown ("Movement") && canMove) {
    11.             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    12.             if (Physics.Raycast (ray, out hit))
    13.                 target = new Vector3 (hit.point.x, characterHeight, hit.point.z);
    14.         }
    15.         if (Vector3.Magnitude (target - transform.position) > 0.3f) {
    16.             if (controller.isGrounded) {
    17.                 moveDirection = (target - transform.position).normalized;
    18.                 moveDirection = transform.TransformDirection (moveDirection);
    19.                 moveDirection *= moveSpeed;
    20.             }
    21.             moveDirection.y -= gravity * Time.deltaTime;
    22.             controller.Move (moveDirection * Time.deltaTime);
    23.         } else
    24.             transform.position = target;
    25.         waypoint.position = target;
    26.     }
    27.  
     
  2. natmaxex

    natmaxex

    Joined:
    Jul 12, 2012
    Posts:
    68
    look at position clicked
     
  3. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    Position Clicked?? I don't understand..
     
  4. natmaxex

    natmaxex

    Joined:
    Jul 12, 2012
    Posts:
    68
  5. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    Thanks, but I'm not quite sure how I can use this. I'm trying to add proper rotation to my character script, but it's a bit tricky when using a character controller for this format of gameplay. Using anything but Move or Simple Move may help add rotation easier, but I will not be using a character controller or collision flags if I'm using another move method like Lerp or MoveTowards. Raycasting would probably be best for collision detection imo.
     
  6. natmaxex

    natmaxex

    Joined:
    Jul 12, 2012
    Posts:
    68
    Oops, I meant:
    https://docs.unity3d.com/ScriptReference/CharacterController.SimpleMove.html
    and
    https://docs.unity3d.com/ScriptReference/CharacterController.Move.html
    mabby