Please Help, I am stuck. I created a script which uses touch input for movement. Although it isn't working on the gameobject i created. Please Help.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour { Rigidbody playerRigidbody; Animator anim; Vector2 touchPosition; Vector3 movement; public float MovementSpeed;
public float animationTime;
float currentTime;
void Start()
{
playerRigidbody = GetComponent<Rigidbody> ();
anim = GetComponent<Animator> ();
}
void Update()
{
currentTime += Time.deltaTime;
Movement ();
if(Input.touchCount >= 2 && Input.GetTouch(1).phase == TouchPhase.Began && currentTime >= animationTime)
{
anim.SetTrigger ("Jump");
currentTime = 0f;
}
}
void FixedUpdate()
{
playerRigidbody.AddForce (movement);
}
void Movement()
{
if(Input.touchCount > 0 && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(0).phase == TouchPhase.Began))
{
touchPosition = Input.GetTouch (0).deltaPosition;
movement = new Vector3 (touchPosition.x*MovementSpeed, 0f, touchPosition.y*MovementSpeed);
}
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
touchPosition = new Vector2 (0f, 0f);
movement = new Vector3 (0f, 0f, 0f);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Is there a way to "rotate" the direction of addForce (without using addRelativeForce)? 0 Answers
Vector3 Help - Calculating velocity toward a point 1 Answer
How do I ignore gravity when using Rigidbody and always have the same jump force? 1 Answer
Rigidbody doesn't seem to apply gravity 1 Answer
How can I move an object in the direction another object is facing. 1 Answer