Question by
nathan-l-lee · Jul 13, 2018 at 06:50 PM ·
c#scripting problemrigidbody
Rigidbody isn't responding to any of my touches
I know that I need to be more specific about my question, but I really can't. In this C# script, I'm trying to control a player with swipes. First in the awake() function, it divides the screen into half, so I can figure out which side the player is swiping. Then in the FixedUpdate() function, it looks for position and measures the x and y lengths of the swipe. Then in the swipeToForce function, it finds the speed of the swipe. Lastly in the implementControl function, it adds torque going the side of the touch and adds forward force. I'm really sorry that I couldn't ask a specific question, but I've been struggling with this for a week and really need help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerControllerSki : MonoBehaviour
{
//general
public Rigidbody Liam;
private Touch touch;
//change aspects of control
public float stabilizer = 100;
public float twistSpeed = 5;
public float swipeTwistSpeed = 5;
public float swipeForce = 20;
//translate to force
//private float forwardForce;
public float swipeOrTap = 440;
//control surfaces
private Rect LeftControlSurface;
private Rect RightControlSurface;
//swipe
private Vector2 startPos;
private Vector2 endPos;
private float diffTime;
private float startTime;
private Vector2 DragDistance;
private float speed;
private float XDist;
private float YDist;
//detection
private bool Swipe;
private bool upDown;
private bool touchCheck;
public float testBoost = 800;
private void Awake()
{
Liam = GetComponent<Rigidbody>();
LeftControlSurface = new Rect(0, 0, Screen.width / 2, Screen.height);
RightControlSurface = new Rect(Screen.width / 2, 0, Screen.width / 2, Screen.height);
//Touch touch = Input.GetTouch(0);
}
void FixedUpdate()
{
if (Input.touchCount > 0) //check amount of touches
{
touchCheck = true;
//all the control scripts
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
Vector2 startPos = touch.position;
float startTime = Time.time;
}
if (touch.phase == TouchPhase.Ended)
{
endPos = touch.position;
diffTime = Time.time - startTime;
startTime = 0;
}
float XDist = Mathf.Abs(startPos.x - endPos.x);
float YDist = Mathf.Abs(startPos.y - endPos.y);
}
}
}
void swipeToForce()
{
//Pythagorean
float DragDistance = Mathf.Sqrt(XDist * XDist + YDist * YDist);
float speed = DragDistance / diffTime;
//Turn swipelength into force
float forwardForce = speed * DragDistance;
}
void implementControl(float forwardForce)
{
if (LeftControlSurface.Contains(touch.position))
{
Liam.AddTorque(0, twistSpeed, 0);
Debug.Log("Left" + "Strength = " + forwardForce + " Speed = " + speed + " Distance = " + DragDistance + " Time = " + diffTime);
Liam.AddForce(transform.forward * forwardForce);
//stick animation and sound here
}
if (RightControlSurface.Contains(touch.position))
{
Liam.AddTorque(0, -twistSpeed, 0);
Debug.Log("Right" + " Strength = " + forwardForce + " Speed = " + speed + " Distance = " + DragDistance + " Time = " + diffTime);
Liam.AddForce(transform.forward * forwardForce);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
make rigidbody jump and maintain velocity 1 Answer
Making a pivot focused game 0 Answers
Modifying mass of all objects 1 Answer
A way to make the player get punished when standing still? 0 Answers