Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Player will not Move

Discussion in 'Getting Started' started by Certas, Aug 22, 2019.

  1. Certas

    Certas

    Joined:
    Aug 19, 2019
    Posts:
    2
    Hello Community.
    I am new here an will learn to Programming Games.

    I have look a Tutorial about Movement like Diablo.
    Script works fine i think, but my Charakter dont move if i click on the ground.

    Here my script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ClickToMove : MonoBehaviour
    6. {
    7.     public float speed;
    8.     public CharacterController controller;
    9.     private Vector3 position;
    10.    
    11.     void Start()
    12.     {
    13.         position = transform.position;
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         if(Input.GetMouseButton(0))
    19.         {
    20.             locatePosition();
    21.         }
    22.     }
    23.    
    24.     void locatePosition()
    25.     {
    26.         RaycastHit hit;
    27.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    28.        
    29.         if(Physics.Raycast(ray, out hit, 1000))
    30.         {
    31.             position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
    32.             Debug.Log(position);
    33.         }
    34.     }
    35.    
    36.     void moveToPosition()
    37.     {
    38.         if(Vector3.Distance(transform.position, position)>1)
    39.         {
    40.             Quaternion newRotation = Quaternion.LookRotation(position-transform.position, Vector3.forward);
    41.            
    42.             newRotation.x = 0f;
    43.             newRotation.z = 0f;
    44.            
    45.             transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
    46.             controller.SimpleMove(transform.forward * speed);
    47.         }
    48.     }
    49. }
    50.  
    I use Unity 2018
    The Tutorial where i have the script are this:


    Thank you for Helping
     
  2. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    130
  3. Certas

    Certas

    Joined:
    Aug 19, 2019
    Posts:
    2
    Oh Thanks! I didnt see it :D