Question by
SunilPalan · Sep 11, 2017 at 03:23 PM ·
c#scripting beginner
My Character randomly stops moving in game.
While in game my character randomly just stops moving, but when i turn the camera it starts to move again (As shown in the video(Hyperlink)). My character's movement depends on the camera rotation. It moves in the direction my camera is facing in.This is the two different scripts I use. Thanks in advanced.
This is the camera rotation.
public class CameraController : MonoBehaviour {
public Transform target;
public Vector3 offset;
public bool useOffsetValues;
public float rotateSpeed;
public Transform pivot;
public float maxViewAngle;
public float minViewAngle;
// Use this for initialization
void Start () {
if(!useOffsetValues)
{
offset = target.position - transform.position;
}
pivot.transform.position = target.transform.position;
pivot.transform.parent = target.transform;
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void LateUpdate ()
{
float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
target.Rotate(0, horizontal, 0);
if (pivot.rotation.eulerAngles.x > maxViewAngle && pivot.rotation.eulerAngles.x < 180f)
{
pivot.rotation = Quaternion.Euler(maxViewAngle, 0, 0);
}
float desiredYAngle = target.eulerAngles.y;
Quaternion rotation = Quaternion.Euler(0, desiredYAngle, 0);
transform.position = target.position - (rotation * offset);
if (transform.position.y < target.position.y)
{
transform.position = new Vector3(transform.position.x, target.position.y +.5f, transform.position.z);
}
transform.LookAt(target);
}
}
This is the Player movement.
public class PlayerController : MonoBehaviour {
public float moveSpeed;
public Rigidbody rig;
public Animator anim;
public string gameOver;
// Use this for initialization
void Start()
{
rig = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
rig.velocity = (transform.forward * moveSpeed);
rig.velocity = rig.velocity.normalized * moveSpeed;
anim.SetFloat("Speed", (Mathf.Abs(moveSpeed)));
}
void OnCollisionEnter(Collision other)
{
if(other.gameObject.tag == "killbox")
{
SceneManager.LoadScene(gameOver);
}
}
}
This is the Video link text
Comment
Your answer
Follow this Question
Related Questions
How do you make your player jump in the Roll-a-Ball tutorial series? 0 Answers
How do I inactivate Text in a script? 2 Answers
HELP! How to make Update function start after delay? C# 2 Answers
please help me, i'm creating a first person game, but my script is incomplete and i can't crouch 0 Answers
Drag and drop a plane coping terrain 0 Answers