- Home /
Trying to get Mecanim to work well with Touch Controls
Any help on getting Mecanim to work well with touch controls? Here's the movement script so far
public class PlayerMovement : MonoBehaviour
{
public float speed = 1f;
public Vector3 touchPoint;
public static bool isMoving = false;
void Update()
{
// Lerps to the last position touched, going to have to modify this
// to only work when tapped not hold cause shooting will be hold.
if (Input.touchCount > 0 )
{
// Shoots a ray where you touched the screen
RaycastHit hit;
// There was an error before. The tag must be main camera.
Ray ray = Camera.main.ScreenPointToRay(Input.touches[0].position);
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
// converts the touch point to unitys 3d worldspace Vector3
touchPoint = hit.point;
}
}
MoveToPos(touchPoint);
}
void MoveToPos(Vector3 touchPoint)
{
isMoving = true;
gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, new Vector3(touchPoint.x, 10, touchPoint.z), speed * Time.deltaTime); // LAWL this had to be z even tho im using y for droid , wasted alot of time on that.
//gameObject.transform.LookAt(touchpoint);
}
}
I want the character to switch from Idle to running. I tried using the sneak tut as a basis for this and got the hashes up but Unity gives an error with the script here:
anim = GetComponent<Animator>();
hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();
anim.SetLayerWeight(1, 1f);
any help would be appreciated.
whats the error? it doesn't look like you have anything accessing the Animator. Your animations aren't going to change unless you set up the animator and access the variables as needed.
anim = GetComponent<Animator>();
hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();
anim.SetLayerWeight(1, 1f);
This piece of code was supposed to access the editor but it gave me these errors:
error CS0103: The name Tags' does not exist in the current context error CS1502: The best overloaded method match for
UnityEngine.GameObject.FindGameObjectWithTag(string)' has some invalid arguments
error CS1503: Argument #1' cannot convert
object' expression to type `string'
Answer by The Ghost · Jun 30, 2013 at 02:52 AM
My solution to this was to use
public Animator anim;
and
anim.SetFloat("Speed", speed); anim.SetFloat("Move", isMoving);
instead.
Your answer
Follow this Question
Related Questions
Controlling a rigged models muscle groups via script (C#) 0 Answers
Moving model with animation 2 Answers
Mecanim avatar moving on x,z 1 Answer
Strange Character Movement (Mecanim) 0 Answers
root motion on in place animation 0 Answers