- Home /
When I enable certain scripts on my player, the transform falls through the map.
Okay, fair warning this is involved.
I have three scripts on my Player controlling movement and input. When the first script which changes lanes was attached, all was well. When I added two more scripts to handle mobile swiping input my player began to fall through the floor. What could be causing this? Ill attach all three separately.
This is the lane change script. All was well when I only had this. Used arrow keys to move player flawlessly. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class LaneSW : MonoBehaviour {
public AudioSource tickSource;
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
public int laneNumber = 0;
public int lanesCount = 4;
bool didChangeLastFrame = false;
public float laneDistance = 2;
public float firstLaneXPos = 0;
public float deadZone = 0.1f;
public float sideSpeed = 5;
void Start()
{
tickSource = GetComponent<AudioSource> ();
}
void Update()
{
//Access the objects Character Controller
CharacterController controller = GetComponent<CharacterController>();
float input = Input.GetAxis("Horizontal");
//Make sure the object is grounded before doing the math for lane changes
if (controller.isGrounded)
if(Mathf.Abs(input) > deadZone) {
if(!didChangeLastFrame) {
didChangeLastFrame = true; //Prevent overshooting lanes
laneNumber += Mathf.RoundToInt(Mathf.Sign(input));
if(laneNumber < 0) laneNumber = 0;
else if(laneNumber >= lanesCount) laneNumber = lanesCount - 1;
}
}
else
{
didChangeLastFrame = false;
moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
Vector3 pos = transform.position;
pos.x = Mathf.Lerp(pos.x, firstLaneXPos + laneDistance * laneNumber, Time.deltaTime * sideSpeed);
transform.position = pos;
moveDirection.y -= gravity * Time.deltaTime;
controller.Move (moveDirection * Time.deltaTime);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up"))
{
tickSource.Play ();
other.gameObject.SetActive (false);
}
}
}
Next two scripts are for handing mobile input. What is causing the player to fall through the map when they're enabled? using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Swipe : MonoBehaviour
{
private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
private bool isDragging = false;
private Vector2 startTouch, swipeDelta;
private void Update()
{
tap = swipeLeft = swipeRight = swipeUp = swipeDown = false;
if(Input.GetMouseButtonDown(0))
{
tap = true;
isDragging = true;
startTouch = Input.mousePosition;
}
else if(Input.GetMouseButtonUp(0))
{
isDragging = false;
Reset();
}
if (Input.touches.Length > 0)
{
if(Input.touches[0].phase == TouchPhase.Began)
{
isDragging = true;
tap = true;
startTouch = Input.touches[0].position;
}
else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
{
isDragging = false;
Reset();
}
}
swipeDelta = Vector2.zero;
if (isDragging)
{
if (Input.touches.Length > 0)
swipeDelta = Input.touches [0].position - startTouch;
else if (Input.GetMouseButton (0))
swipeDelta = (Vector2)Input.mousePosition - startTouch;
}
if (swipeDelta.magnitude > 125)
{
// Which direction?
float x = swipeDelta.x;
float y = swipeDelta.y;
if (Mathf.Abs (x) > Mathf.Abs (y))
{
// Left or right
if (x < 0)
swipeLeft = true;
else
swipeRight = true;
}
else
{
// Up or down
if (y < 0)
swipeDown = true;
else
swipeUp = true
}
Reset ();
}
}
private void Reset()
{
startTouch = swipeDelta = Vector2.zero;
isDragging = false;
}
public Vector2 SwipeDelta { get { return swipeDelta; } }
public bool SwipeLeft { get { return swipeLeft; } }
public bool SwipeRight { get { return swipeRight; } }
public bool SwipeUp { get { return swipeUp; } }
public bool SwipeDown { get { return swipeDown; } }
}
AND using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SwipeTest : MonoBehaviour
{
public Swipe swipeControls;
public Transform player;
private Vector3 desiredPosition;
private void Update()
{
if (swipeControls.SwipeLeft)
desiredPosition += Vector3.left;
if (swipeControls.SwipeRight)
desiredPosition += Vector3.right;
if (swipeControls.SwipeUp)
desiredPosition += Vector3.forward;
if (swipeControls.SwipeDown)
desiredPosition += Vector3.back;
player.transform.position = Vector3.MoveTowards(player.transform.position, desiredPosition, 3f * Time.deltaTime);
}
}
How about keeping the first with one more. Then check. Then remove second and add third.
Changing a lot and trying to figure what caused a fault is tricky. Narrow it down.
Your answer
Follow this Question
Related Questions
How to move player or character Left right by 14 unit every at button click on mobile 0 Answers
How to move your player or character left or right by 14 unit on button click in mobile 0 Answers
OnPointerDown and OnPointerUp doesn't work 6 Answers
Raycast and colliders problem. Corners of two colliders 0 Answers
Distribute terrain in zones 3 Answers