Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Player movement (Goes through things)

Discussion in 'Scripting' started by Luke3671, Dec 13, 2018.

  1. Luke3671

    Luke3671

    Joined:
    Jul 6, 2014
    Posts:
    56
    FIXED

    Learning Unity and its char movement system with the animator.
    I have this atm but when playing my animator & char is working/moving how it should be work but my whole char goes through things even with rigidbody active.
    is there something i'm missing?
    code is below.

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class AJControls : MonoBehaviour
    5. {
    6.     static Animator anim;
    7.     public float speed = 10.0F;
    8.     public float rotationSpeed = 100.0f;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         anim = GetComponent<Animator>();
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.  
    20.         float translation = Input.GetAxis("Vertical") * speed;
    21.         float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
    22.         translation *= Time.deltaTime;
    23.         rotation *= Time.deltaTime;
    24.         transform.Translate(0, 0, translation);
    25.         transform.Rotate(0, rotation, 0);
    26.  
    27.         if (Input.GetButtonDown("Jump"))
    28.         {
    29.             anim.SetTrigger("isJumping");
    30.         }
    31.  
    32.         if (translation != 0)
    33.         {
    34.             anim.SetBool("isRunning", true);
    35.         }
    36.         else
    37.         {
    38.             anim.SetBool("isRunning", false);
    39.         }
    40.  
    41.     }
    42. }
    Thank you
     
    Last edited: Dec 13, 2018
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Unless both objects have a collider and a rigidbody component that is NOT set to "IsKinematic"(unless it is the stationary object) it won't expirence any type of physics.
     
    Luke3671 likes this.
  3. Luke3671

    Luke3671

    Joined:
    Jul 6, 2014
    Posts:
    56
    Thank you that fixed it.