- Home /
Question by
matthew_gigante · Sep 05, 2019 at 12:37 AM ·
c#androidmobilescriptingproblem
Moving an object in Unity3d for Android
I am trying to drag around an object similarly to in this video: https://www.youtube.com/watch?v=eSdjNGNj6uk
That video is made for 2d, but I am using 3d, and it is from a top-down perspective so I would be moving around an object with X and Z. Also worth noting I have constrained the Y position, and the X and Y rotations. Here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScoopScript : MonoBehaviour
{
private float deltaX, deltaZ;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
private void Update()
{
if(Input.touchCount > 0) {
Touch touch = Input.GetTouch(0);
Vector3 touchPos = Camera.main.ScreenToWorldPoint(touch.position);
switch (touch.phase)
{
case TouchPhase.Began:
deltaX = touchPos.x - transform.position.x;
deltaZ = touchPos.z - transform.position.z;
break;
case TouchPhase.Moved:
rb.MovePosition(new Vector3(touchPos.x - deltaX, transform.position.y, touchPos.z - deltaZ));
break;
case TouchPhase.Ended:
rb.velocity = Vector3.zero;
break;
}
}
}
}
When the game is played, the cube does not move, when I had no constraints it would spin around constantly, any suggestions are appreciated.
Comment
Your answer
Follow this Question
Related Questions
System.Diagnostics.Process on iOs and Android 1 Answer
Developing on multiple resolutions 1 Answer
Button as a child of a button 0 Answers
Android IL2CPP. Show "Strip Code Engine" status in the .apk build. 0 Answers
c# or java for mobile version 2 Answers