Bug Player spinning on play

Discussion in 'Game Development' started by ExGamer, Jul 16, 2017 with 1 replies and 388 views.

  1. ExGamer

    ExGamer Enthusiast

    Messages:
    156
    Ratings:
    31
    As soon as i hit play game My player continuously spins around and don't know what the source of the problem is. This is in unity 5 and in C#.

    Here is the player script.

    Code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class ClickToMove : MonoBehaviour {
        public float speed;
        public CharacterController controller;
         private Vector3 position;
        // Use this for initialization
        void Start () {
            
        }
        
        // Update is called once per frame
        void Update () {
            if(Input.GetMouseButton(0)) {
                Locate();
            }
    
            MoveToPosition();
    
        }
        void Locate()
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if(Physics.Raycast(ray, out hit, 1000))
            {
                position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
                Debug.Log(position);
            }
        }
    
        void MoveToPosition()
        {
            Quaternion newRotation = Quaternion.LookRotation(position-transform.position);
            newRotation.x = 0f;
            newRotation.z = 0f;
            controller.SimpleMove(transform.forward);
            transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
        }
    }
    
     
  2. Xolitude

    Xolitude Enthusiast

    Messages:
    108
    Ratings:
    15
    I've worked in Unity a few times and have worked with something similar, and all I can notice is MoveToPosition() is being called every frame rather than only being called when you click a position. In the MoveToPosition() method, you're updating the transforms rotation every frame. Try to update it only when you click.
     

Share This Page