- Home /
Question by
iPoopLegos · Jul 09, 2016 at 07:33 AM ·
c#bugcharactercontrollerplayer movementglitch
Why is this code glitching?
Whenever I use this code, the player character is always glitching, by teleporting to the left and right of the screen extremely fast. Can anybody explain what i'm doing wrong?
using UnityEngine;
using System.Collections;
using System;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
//xMin = 4, xMax = -4, zMin = 2.39, and zMax = 3.84
}
public class PlayerContoller : MonoBehaviour
{
public float speed;
public float tilt;
public Boundary boundary;
public Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.velocity = movement * speed;
rb.position = new Vector3
(
Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax),
0.0f
Mathf.Clamp(rb.position.z, boundary.zMin, boundary.zMax)
);
rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * -tilt);
}
}
Speed = 5
Comment
Best Answer
Answer by tanoshimi · Jul 12, 2016 at 01:22 PM
Your xMin and xMax values are the wrong way around.
//xMin = 4, xMax = -4
Answer by AnneSchmidt_legacy · Jul 09, 2016 at 08:08 AM
Hi,
It's
rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
not
rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * -tilt)'
That was just a glitch from when I re-typed it, in case I found the mistake whilst I was typing it. So it's still glitching.