How do i make the player to be "Idle" when reaching the mouse position ?
And what is this bug thing in this line ?
idleState = Animator.StringToHash("Idle");
This is not my script not all the code is my script.
In my project in Window > Animator i created two states called them: Walk and Idle for the Walk i'm using the HumanoidWalk animation and for the Idle the HumanoidIdle.
Then i added to the script that when i click the mouse the player walk animation will start. Then when the player is getting to the mouse position he stop moving but the Walk animation is keep animated so whet i see is the player walking on place.
What i want to do is when the player reached the target position make him "Idle".
But can't find where and how to do it.
using UnityEngine;
using System.Collections;
public class PlayerMovements : MonoBehaviour {
private Transform myTransform; // this transform
private Vector3 destinationPosition; // The destination Point
private float destinationDistance; // The distance between myTransform and destinationPosition
public float Speed; // Speed at which the character moves
public float Direction; // The Speed the character will move
public float moveAnimSpeed; // Float trigger for Float created in Mecanim (Use this to trigger transitions.
public float animSpeed = 1.5f; // Animation Speed
public Animator anim; // Animator to Anim converter
public int idleState = Animator.StringToHash("Base Layer.Idle"); // String to Hash conversion for Mecanim "Base Layer"
public int runState = Animator.StringToHash("Base Layer.Run"); // String to Hash for Mecanim "Base Layer Run"
private AnimatorStateInfo currentBaseState; // a reference to the current state of the animator, used for base layer
private Collider col;
void Start()
{
Physics.gravity = new Vector3(0, -200f, 0); // used In conjunction with RigidBody for better Gravity (Freeze Rotation X,Y,Z), set mass and use Gravity)
anim = GetComponent<Animator>();
idleState = Animator.StringToHash("Idle"); // Duplicate added due to Bug
runState = Animator.StringToHash("Run");
myTransform = transform; // sets myTransform to this GameObject.transform
destinationPosition = myTransform.position;
// prevents myTransform reset
}
void FixedUpdate()
{
// keep track of the distance between this gameObject and destinationPosition
currentBaseState = anim.GetCurrentAnimatorStateInfo(0);
destinationDistance = Vector3.Distance(destinationPosition, myTransform.position);
// Set's speed in reference to distance
if (destinationDistance < .5f)
{
Speed = 2;
}
else if (destinationDistance > .5f)
{
Speed = 2;
//Below sends Floats to Mecanim, Raycast set's speed to X until destination is reached animation is played until speed drops
}
if (Speed > .5f)
{
anim.SetFloat("moveAnimSpeed", 0.1f);
}
else if (Speed < .5f)
{
anim.SetFloat("moveAnimSpeed", 0.1f);
} //
// Moves the Player if the Left Mouse Button was clicked
if (Input.GetMouseButtonDown(0) && GUIUtility.hotControl == 0)
{
anim.CrossFade("Walk", 0);
Plane playerPlane = new Plane(Vector3.up, myTransform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist))
{
Vector3 targetPoint = ray.GetPoint(hitdist);
destinationPosition = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
myTransform.rotation = targetRotation;
anim.CrossFade("Idle", 0);
}
}
// Moves the player if the mouse button is hold down
else
if (Input.GetMouseButton(0) && GUIUtility.hotControl == 0) {
anim.CrossFade("Walk", 0);
Plane playerPlane = new Plane(Vector3.up, myTransform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist))
{
Vector3 targetPoint = ray.GetPoint(hitdist);
destinationPosition = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
myTransform.rotation = targetRotation;
}
// myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
}
// To prevent code from running if not needed
if (destinationDistance > .5f)
{
myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, Speed * Time.deltaTime);
}
}
}
Answer by timdouglas90 · Oct 25, 2016 at 11:33 AM
In your code for moving the player if the mouse is held down, modify your if to include a check for the mouse position relative to the player position, if they are equal set the player to idle.
https://docs.unity3d.com/ScriptReference/Input-mousePosition.html <- this is the method that returns the coordinates of the mouse. So you should check the players transform.position relative to the mouses :)
If you need more help with how to code that then let me know!
I changed the secon IF the one with the hold down to this:
if (Input.Get$$anonymous$$ouseButton(0) && GUIUtility.hotControl == 0 && myTransform.position == Input.mousePosition) {
anim.CrossFade("Idle", 0);
And i removed the Walk from this one and have Walk on only on the first IF.
But now the player is moving to the mouse point there is no walk animation at all.
Could you paste the code you have now? Just the section you edited.
if (Input.Get$$anonymous$$ouseButton(0) && GUIUtility.hotControl == 0 && myTransform.position == Input.mousePosition) {
anim.CrossFade("Idle", 0);
Plane playerPlane = new Plane(Vector3.up, myTransform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist))
{
Vector3 targetPoint = ray.GetPoint(hitdist);
destinationPosition = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
myTransform.rotation = targetRotation;
}
No.
That check will never work unless the player's world position happens to be exactly the mouse coordinates in screenspace.
You need to convert coordinate into worldspace using either the built-in camera functions or your own conversion, but this is not an answer.
$$anonymous$$oreover: Input.mousePosition returns a Vector2, whereas transform position is a Vector3. The types aren't even correct.
This is my original script i asked about it before but still not understand how to solve it.
First i'm using RaycastHit and Ray and LookAt this to make my character(ThirdPersonController) in Idle mode so i will be able to rotate the player without moving it and he will rotate and face the mouse cursour when i move the mouse around the game screen. This part is working.
Then i'm doing a check if clicking the mouse then walk to the mouse position and when getting to the mouse position Idle again.
But when the player is getting to the mouse point i want it to idle like in the beginning using the RaycastHit Ray and LookAt i mean the player will not move but i will be able to move the mouse cursour again and the player will rotate and face the mouse cursour. But i can't make it right this part.
When i click the mouse the character is walking to the mouse position point but when he reach it he start rotating like crazy on place and then if i move my mouse around the character keep walking after the mouse. But this is not what i want. I want when the character reached the mouse position then don't move/walk just to be idle again and to be able to move the mouse cursour again and that the player will rotate and face the mouse cursour until i will click the mouse again.
Can't solve this:
using UnityEngine;
using System.Collections;
public class $$anonymous$$oveObjects : $$anonymous$$onoBehaviour {
private CharacterController cc;
public float moveSpeed = 1.0f;
public float smooth = 1f;
public float pathLength = 5f;
private float distanceTravelled;
private Vector3 startPositon;
private Vector3 targetAngles;
private Animator _animator;
bool isSpinning;
bool isWalking;
// Use this for initialization
void Start()
{
isWalking = true;
_animator = GetComponent<Animator>();
_animator.CrossFade("Idle", 0);
}
// Update is called once per frame
void Update()
{
$$anonymous$$ovePlayerWith$$anonymous$$ouse();
}
private void $$anonymous$$ovePlayerWith$$anonymous$$ouse()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit) && hit.collider.name != "ThirdPersonController")
{
transform.LookAt(hit.point);
}
else
{
transform.LookAt(ray.GetPoint(100)); //the number here is compltely arbitrary
}
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
if ((transform.position - hit.point).magnitude < 1.0f)
{
_animator.CrossFade("Idle", 0);
}
else
{
_animator.CrossFade("Walk", 0);
}
}
}
Well you are telling the computer to do just that. The entire Raycast / Rotate logic is happening every frame right now, regardless of whether or not your mouse is being clicked. Here, your mouse is only controlling the animator.
Additionally, there isn't anything in here that would move your character. I see that it's in the script pasted for your original post, but it's somewhat sloppy to control rotations / position in different places.
Your answer
Follow this Question
Related Questions
I create material with script but it does not render right 0 Answers
Creating Splines from empties in script 0 Answers
how to ask if something is not true for an amount of time 2 Answers
How can i check if the alpha color or the material color is transparent ? 1 Answer
How do I make somthing happen when the Player reaches a certain x, y, z position? 0 Answers