- Home /
Input Issue
Just to warn you i am completely new to scripting, to bear with me here.
So I am trying to build some mechanics for a 3D top down RPG with click to move/attack elements. I'm trying to get some mechanics down so I started with a click to move and a right click to auto attack. creating the left click to move script turned out fine, if a little floaty, but trying to create a right click to auto attack has me stumped. whenever I try to create an input for the right mouse button, I get an error that says, "The name 'input' does not exist in the current context," while when i used the same thing for click to move, it worked fine.
here is my click to move script:
using UnityEngine;
using System.Collections;
public class ClicktoMove : MonoBehaviour
{
// the navmesh
NavMeshAgent agent;
//calls the navmesh
void Start ()
{
agent = GetComponent<NavMeshAgent>();
}
void Update ()
{
Move ();
}
//Player Movement Method
//left click to move
void Move()
{
if (Input.GetMouseButtonUp (0))
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 100))
{
agent.SetDestination (hit.point);
}
}
}
}
and here is my auto attack. I stopped when the error appeared, so there isn't an action associated with my if statement (yet).
using UnityEngine;
using System.Collections;
public class PlayerStats : MonoBehaviour
{
// Player's Stats
//--------------------------------
public string name = "Korhol";
public int health;
public int damage;
public float range;
//public float attackSpeed;(not in use)
//--------------------------------
// Enemy Script
//public transform opponent;(not in use)
public EnemyStats opponent;
//--------------------------------
//end variables
void Start ()
{
}
void Update ()
{
}
void autoAttack()
{
if (Input.getMouseButtonUp(1)) //<----- problem child
{
}
}
}
i would apreciate it if someone could help me out with this and explain why it worked of my moving script but not my attacking script
Answer by The_Toller · Jun 11, 2015 at 09:33 AM
You're problem is that you're trying to call the getMouseButtonUp method from outside the update function.
"You need to call this function from the Update function, since the state gets reset each frame. It will not return true until the user has pressed the mouse button and released it again. button values are 0 for left button, 1 for right button, 2 for the middle button." -Unity scripting API
You have set the check for the mouse event in a method you never call and you need it in the update function! :) Try this solution instead and you'll be fine!
using UnityEngine;
using System.Collections;
public class PlayerStats : MonoBehaviour
{
// Player's Stats
//--------------------------------
public string name = "Korhol";
public int health;
public int damage;
public float range;
//public float attackSpeed;(not in use)
//--------------------------------
// Enemy Script
//public transform opponent;(not in use)
public EnemyStats opponent;
//--------------------------------
//end variables
void Start ()
{
}
void Update ()
{
if(Input.GetMouseButtonUp(1))
{
AutoAttack();
}
}
void AutoAttack()
{
}
}
In your first script, you're calling your input check method from the update function. However, you're not doing that in your PlayerStats. This solution is more like your first script and would also work:
void Update ()
{
AutoAttack();
}
void AutoAttack()
{
if(Input.GetMouseButtonUp(1))
{
}
}
}
Have a nice day, and good luck with your game!
Your answer
Follow this Question
Related Questions
Input button is not setup?? 2 Answers
Input manager cross platform 2 Answers
UnityException: Input Axis Rotate2 is not setup. 0 Answers