- Home /
Question by
panim0_unity · Sep 28, 2019 at 12:18 PM ·
c#unity 5cameratouchmovement script
Camera up and down on TouchField
Here's the touch field script attached to the image o the canvas
public class FixedTouchField : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
[HideInInspector]
public Vector2 TouchDist;
[HideInInspector]
public Vector2 PointerOld;
[HideInInspector]
protected int PointerId;
[HideInInspector]
public bool Pressed;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Pressed)
{
if (PointerId >= 0 && PointerId < Input.touches.Length)
{
TouchDist = Input.touches[PointerId].position - PointerOld;
PointerOld = Input.touches[PointerId].position;
}
else
{
TouchDist = new Vector2(Input.mousePosition.x, Input.mousePosition.y) - PointerOld;
PointerOld = Input.mousePosition;
}
}
else
{
TouchDist = new Vector2();
}
}
public void OnPointerDown(PointerEventData eventData)
{
Pressed = true;
PointerId = eventData.pointerId;
PointerOld = eventData.position;
}
public void OnPointerUp(PointerEventData eventData)
{
Pressed = false;
}
}
and my character and camera control script here
public class LionControl : MonoBehaviour
{
public FixedJoystick LeftJoystick;
public FixedButton Button;
public FixedTouchField TouchField;
protected LionActions actions;
protected LionPlayerControls lionPlayerControls;
protected Rigidbody Rigidbody;
protected float CameraAngleY;
protected float CameraAngleSpeed = 0.1f;
protected float CameraAngleX;
protected float CameraPosY;
protected float CameraPosSpeed = 0.1f;
// Start is called before the first frame update
void Start()
{
actions = GetComponent<LionActions>();
lionPlayerControls = GetComponent<LionPlayerControls>();
Rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
var input = new Vector3(LeftJoystick.inputVector.x, 0, LeftJoystick.inputVector.y);
var vel = Quaternion.AngleAxis(CameraAngleY + 180, Vector3.up) * input * 20f;
Rigidbody.velocity = new Vector3(vel.x, Rigidbody.velocity.y, vel.z)*-1;
transform.rotation = Quaternion.AngleAxis(CameraAngleY+Vector3.SignedAngle(Vector3.forward,input.normalized,Vector3.up), Vector3.up);
CameraAngleY += TouchField.TouchDist.x * CameraAngleSpeed;
Camera.main.transform.position = transform.position + Quaternion.AngleAxis(CameraAngleY, Vector3.up) * new Vector3(0, 12, -15);
Camera.main.transform.rotation = Quaternion.LookRotation(transform.position + Vector3.up * 2f - Camera.main.transform.position, Vector3.up);
if (Rigidbody.velocity.magnitude > 3f)
actions.Run();
else
actions.Stay();
if (Input.GetKeyDown(KeyCode.Space))
actions.Jump();
}
public void JumpLion()
{
actions.Jump();
}
}
so there are few problems that I need some help for. First character rotates with the cameras location together but I want it to be like this; you'll have a free look around the player untill you move it and character should rotate to the cameras rotation when start moving. Second I want to be able to move camera on the X so I can look up and down on the character. How can you help me with this , codes or some guidence please
Comment