Question by
eirikamb · Jun 26, 2016 at 06:02 AM ·
c#rotationthird person controller
My character starts spinning, what is wrong whit this two scripts?
I use two script to get a camera relative movemnt on my third person character... But after I put in the Jump function the caracther started snap rotate and floats in the air.... If you know about another way to get a camera relative movement in c# I would also be interrested n that... Thanks for all answers
using UnityEngine; using System.Collections;
[RequireComponent(typeof(CharacterController))] public class RelativMovement : MonoBehaviour { [SerializeField] private Transform target;
public float moveSpeed = 6.0f;
public float jumpsSpeed = 15.0f;
public float gravity = -9.8f;
public float terminalVelocity = -10.0f;
public float minfall = -1.5f;
private float _vertSpeed;
private CharacterController _charController;
// Use this for initialization
void Start () {
_charController = GetComponent<CharacterController>();
_vertSpeed = minfall;
}
// Update is called once per frame
void Update () {
Vector3 movement = Vector3.zero;
float horInput = Input.GetAxis("Horizontal");
float vertInput = Input.GetAxis("Vertical");
if (_charController.isGrounded)
{
if (Input.GetButtonDown("Jump"))
{
_vertSpeed = minfall;
}
else
{
_vertSpeed += gravity * 5 * Time.deltaTime;
if(_vertSpeed < terminalVelocity)
{
_vertSpeed = terminalVelocity;
}
}
movement.y = _vertSpeed;
movement *= Time.deltaTime;
_charController.Move(movement);
}
if (horInput != 0 || vertInput != 0)
{
movement.x = horInput * moveSpeed;
movement.z = vertInput * moveSpeed;
movement = Vector3.ClampMagnitude(movement, moveSpeed);
Quaternion tmp = target.rotation;
target.eulerAngles = new Vector3(0, target.eulerAngles.y, 0);
movement = target.TransformDirection(movement);
target.rotation = tmp;
transform.rotation = Quaternion.LookRotation(movement);
}
movement *= Time.deltaTime;
_charController.Move(movement);
}
}
Comment