- Home /
Player Stopping Suddenly?
I have been trying hard to fix this, but I cant figure out any solution.
This is in in the editor:
This is in the build in mobile phone:
As you can see the sphere is stopping minutely after a certain amount of time in the build. These are the Scripts:
This is the movement Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public GameObject gun;
public int speed = 10;
public Rigidbody rb;
float h;
float v;
public Joystick joystick;
// Update is called once per frame
void FixedUpdate()
{
h = joystick.Horizontal;
v = joystick.Vertical;
GetComponent<Rigidbody>().AddForce(Vector3.forward * speed * v);
GetComponent<Rigidbody>().AddForce(Vector3.right * speed * h);
rb.velocity = rb.velocity * (1 - Time.deltaTime * rb.drag);
}
private void Update()
{
gun.transform.position = transform.position;
}
}
This is the gun script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun : MonoBehaviour
{
public Joystick joystick;
float yaw;
public float LerpSpeed;
private float finalYaw;
public float turnSpeed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
finalYaw = Mathf.Lerp(finalYaw, yaw, Time.deltaTime * LerpSpeed);
yaw += joystick.Horizontal * turnSpeed;
transform.eulerAngles = new Vector3(transform.rotation.x, -finalYaw, transform.rotation.y);
}
}
This is the Camera Script(I copied it from the internet):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMovement : MonoBehaviour
{
public GameObject player; //Public variable to store a reference to the player game object
private Vector3 offset; //Private variable to store the offset distance between the player and camera
// Use this for initialization
void Start()
{
//Calculate and store the offset value by getting the distance between the player's position and camera's position.
offset = transform.position - player.transform.position;
}
// LateUpdate is called after Update each frame
void LateUpdate()
{
// Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
transform.position = player.transform.position + offset;
}
}
This is My Hierarchy:
As you can guess the parent "Player" is not moving. The sphere is moving and the gun is simply following the position of the sphere.
I don't understand why the sphere is stopping minutely after a certain amount of time. The sphere is not stopping because of sphere is colliding with the gun. The gun is at a considerable distance form the sphere. Can Some one please help me.
Your answer
![](https://koobas.hobune.stream/wayback/20220613021017im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to stop a moving 2D player ? 1 Answer
Stopping all player interactions with OnCollisionEnter? 2 Answers
Player jump not working correctly 1 Answer