News › Forums › Sentio Characters › General Discussion and Troubleshooting › Help: Scripting Animation for Walking
This topic contains 1 reply, has 2 voices, and was last updated by prime 1 month ago.
-
AuthorPosts
-
April 28, 2022 at 7:29 pm #37761
Hello Community,
I am trying to use the Sentrino Necromancer model as a main player control character with click to move as a control. I am able to get the model to move in the C# script below but when I try to use Component<Animation>() to allow him to walk2 it says it can not find it. What am I doing wrong and how should I correct this? Is there a prebuilt walking script I am overlooking with the animation already in there? I am new to game development and I am using Unity 5. Thanks in Advance!
Hello Community,
I am trying to use the Sentrino Necromancer model as a main player control character with click to move as a control. I am able to get the model to move in the C# script below but when I try to use Component<Animation>() to allow him to walk2 it says it can not find it. What am I doing wrong and how should I correct this? Is there a prebuilt walking script I am overlooking with the animation already in there? I am new to game development and I am using Unity 5. Thanks in Advance!
using UnityEngine; using System.Collections; public class ClickToMove3 : MonoBehaviour { private Transform myTransform; // this transform private Vector3 destinationPosition; // The destination Point private float destinationDistance; // The distance between myTransform and destinationPosition public float moveSpeed; // The Speed the character will move Animator anim; int walk = Animator.StringToHash("walk2"); void Start() { myTransform = transform; // sets myTransform to this GameObject.transform destinationPosition = myTransform.position; // prevents myTransform reset // My animation anim = GetComponent<Animator>(); } void Update() { // My Animation float move = Input.GetAxis("Vertical"); anim.SetFloat("Speed", move); AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo(0); // keep track of the distance between this gameObject and destinationPosition destinationDistance = Vector3.Distance(destinationPosition, myTransform.position); if (destinationDistance < .5f) { // To prevent shakin behavior when near destination moveSpeed = 0; } else if (destinationDistance > .5f) { // To Reset Speed to default moveSpeed = 3; anim.SetTrigger(walk); } // Moves the Player if the Left Mouse Button was clicked if (Input.GetMouseButtonDown(0) && GUIUtility.hotControl == 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.SetTrigger(walk); } // Moves the player if the mouse button is hold down else if (Input.GetMouseButton(0) && GUIUtility.hotControl == 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, moveSpeed * Time.deltaTime); } } }
May 3, 2022 at 8:51 am #37811I probably wouldn’t try to control movement and animation through code. That’s part of what RAIN is intended to do for you. Your best bet is to replace the behavior tree to respond to mouse click events by setting a move target.
Instead of this:
// Moves the Player if the Left Mouse Button was clicked if (Input.GetMouseButtonDown(0) && GUIUtility.hotControl == 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.SetTrigger(walk); }
do something like this:
AIRig aiRig = GetComponentInChildren<AIRig>(); // Moves the Player if the Left Mouse Button was clicked if (Input.GetMouseButtonDown(0) && GUIUtility.hotControl == 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); aiRig.AI.WorkingMemory.SetItem<Vector3>("moveTarget", targetPoint); //you will have to get } }
and then create your own behavior tree that uses a Move node to go to the moveTarget.
-
AuthorPosts
You must be logged in to reply to this topic.