Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Character not moving, why?? (Resolved)

Discussion in 'Scripting' started by Griftenatt, Mar 27, 2015.

  1. Griftenatt

    Griftenatt

    Joined:
    Mar 27, 2015
    Posts:
    15
    Hi, can understund why my character is not moving, it rotate but not move. Its a clickt o move script. Any idea? :)


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class ClickToMove : MonoBehaviour {
    4.  
    5. public float speed;
    6. private Vector3 position;
    7.  
    8. // Use this for initialization
    9. void Start () {
    10. position = transform.position;
    11. }
    12. // Update is called once per frame
    13. void Update () {
    14. if(Input.GetMouseButton(0)){
    15. LocatePosition();
    16. }
    17.  
    18.  
    19. MoveToPosition ();
    20. }
    21.  
    22.  
    23. void LocatePosition()
    24. {
    25. RaycastHit hit;
    26. Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    27.  
    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.  
    37. void MoveToPosition()
    38. {
    39. if(Vector3.Distance(transform.position,position) > 1)
    40.   {
    41. Quaternion newRotation = Quaternion.LookRotation (position - transform.position);
    42. newRotation.x = 0f;
    43. newRotation.z = 0f;
    44. transform.rotation = Quaternion.Slerp (transform.rotation, newRotation, Time.deltaTime * 10);
    45.  
    46.  
    47. GetComponent<CharacterController>().SimpleMove (transform.forward *speed);
    48.   }
    49. }
    50. }
     
  2. Griftenatt

    Griftenatt

    Joined:
    Mar 27, 2015
    Posts:
    15
    Resolved, what wrong values in the CharacterController :)