Question by
BrekWorld_official · Sep 08, 2019 at 09:15 PM ·
unity 5unity android
Character Spinning Issue
When character slightly colliding with child object, my character starts to spin. https://imgur.com/a/gXk7q79 I picked up script from asset store and slightly edit it.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
public class PlayerMoveController : MonoBehaviour {
// PUBLIC
public SimpleTouchController leftController;
public SimpleTouchController rightController;
public Transform headTrans;
public float speedMovements = 5f;
public float speedContinuousLook = 100f;
public float speedProgressiveLook = 3000f;
// PRIVATE
private Rigidbody _rigidbody;
[SerializeField] bool continuousRightController = true;
void Awake()
{
_rigidbody = GetComponent<Rigidbody>();
rightController.TouchEvent += RightController_TouchEvent;
}
public bool ContinuousRightController
{
set{continuousRightController = value;}
}
void RightController_TouchEvent (Vector2 value)
{
if(!continuousRightController)
{
UpdateAim(value);
}
}
public void move_Forward()
{
_rigidbody.AddForce(new Vector3(transform.localPosition.x, transform.localPosition.y, transform.localPosition.z * -speedMovements));
}
void Update()
{
// move
_rigidbody.MovePosition(transform.position + (transform.forward * leftController.GetTouchPosition.y * Time.deltaTime * speedMovements) +
(transform.right * leftController.GetTouchPosition.x * Time.deltaTime * speedMovements) );
if(continuousRightController)
{
UpdateAim(rightController.GetTouchPosition);
}
}
void UpdateAim(Vector2 value)
{
if(headTrans != null)
{
Quaternion rot = Quaternion.Euler(0f,
transform.localEulerAngles.y - value.x * Time.deltaTime * -speedProgressiveLook,
0f);
_rigidbody.MoveRotation(rot);
rot = Quaternion.Euler(headTrans.localEulerAngles.x - value.y * Time.deltaTime * speedProgressiveLook,
0f,
0f);
headTrans.localRotation = rot;
}
else
{
Quaternion rot = Quaternion.Euler(transform.localEulerAngles.x - value.y * Time.deltaTime * speedProgressiveLook,
transform.localEulerAngles.y + value.x * Time.deltaTime * speedProgressiveLook,
0f);
_rigidbody.MoveRotation(rot);
}
}
void OnDestroy()
{
rightController.TouchEvent -= RightController_TouchEvent;
}
}
Comment
Your answer
Follow this Question
Related Questions
Sound plays randomly during scene 1 Answer
Kill animation between 2 diferent characters 1 Answer
Count the number of fingers of leap motion in Unity5? 1 Answer
Unity 5 totally bugged 0 Answers