Szerző Téma: Unity Collision mozogjon az animációval  (Megtekintve 842 alkalommal)

Unity Collision mozogjon az animációval
« Dátum: 2017. december 12. - 17:46:51 »
0 Show voters
Sziasztok!
 
Unityben van egy karakterem, rajta Character Controller. Ez nagyon jó, de ha ugrik a karakter a collision a földön marad* és az nekem nem jó. Mi lenne a megoldás?
 
*Mármint valamennyire felugrik a collision, de maga az animáció magasabbra ugrik.
 
PlayerController.cs
 

using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float walkSpeed = 2;
public float runSpeed = 6;
public float gravity = -12;
public float jumpHeight = 1;
[Range(0,1)]
public float airControlPercent;
public float turnSmoothTime = 0.2f;
float turnSmoothVelocity;
public float speedSmoothTime = 0.1f;
float speedSmoothVelocity;
float currentSpeed;
float velocityY;
Animator animator;
Transform cameraT;
public CharacterController controller;
void Start () {
   animator = GetComponent<Animator> ();
   cameraT = Camera.main.transform;
   controller = GetComponent<CharacterController> ();
}
void Update () {
   // input
   Vector2 input = new Vector2 (Input.GetAxisRaw (\"Horizontal\"), Input.GetAxisRaw (\"Vertical\"));
   Vector2 inputDir = input.normalized;
   bool running = Input.GetKey (KeyCode.LeftShift);
   Move (inputDir, running);
   if (Input.GetKeyDown (KeyCode.Space)) {
      Jump ();
   }
   // animator
   float animationSpeedPercent = ((running) ? currentSpeed / runSpeed : currentSpeed / walkSpeed * .5f);
   animator.SetFloat (\"speedPercent\", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
}
void Move(Vector2 inputDir, bool running) {
   if (inputDir != Vector2.zero) {
      float targetRotation = Mathf.Atan2 (inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
      transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));
   }
      
   float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
   currentSpeed = Mathf.SmoothDamp (currentSpeed, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));
   velocityY += Time.deltaTime * gravity;
   Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
   controller.Move (velocity * Time.deltaTime);
   currentSpeed = new Vector2 (controller.velocity.x, controller.velocity.z).magnitude;
   if (controller.isGrounded) {
      velocityY = 0;
   }
}
void Jump() {
   if (controller.isGrounded) {
      float jumpVelocity = Mathf.Sqrt (-2 * gravity * jumpHeight);
      velocityY = jumpVelocity;
      animator.Play(\"JUMP00\");
   }
}
float GetModifiedSmoothTime(float smoothTime) {
   if (controller.isGrounded) {
      return smoothTime;
   }
   if (airControlPercent == 0) {
      return float.MaxValue;
   }
   return smoothTime / airControlPercent;
}
}

 
 
« Utoljára szerkesztve: 2017. december 12. - 17:54:31 írta thegergo02 »

Unity Collision mozogjon az animációval
« Válasz #1 Dátum: 2017. december 12. - 18:00:29 »
0 Show voters
Nem is tudom mire van a jumpHeight...
 
MEGOLDVA

 

SimplePortal 2.3.7 © 2008-2024, SimplePortal