Bug Player spinning on play

ExGamer

Enthusiast
Messages
155
Reaction score
32
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);
    }
}
 

Xolitude

Enthusiast
Messages
108
Reaction score
21
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);
    }
}
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.
 
Top Bottom