Move NPC on triggerEnter
So my issue is I have a raindeer in a cage as a level goal, when Player jumps on cage the door opens and th raindeer should run out. I have a box collider on top of the cage set as trigger, animator contollers on both cage and raindeer and rigidbody on raindeer. Trigger will play cage open animation and will change raindeer animation from idle to run but the raindeer won't move and just runs on the spot. This is my code :
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ReindeerCage : MonoBehaviour { Animator _anim; Animator raindeerAnim; public GameObject raindeer; Rigidbody rB; public float xVel; // Start is called before the first frame update void Start() { _anim = GetComponent<Animator>(); raindeerAnim = raindeer.GetComponent<Animator>(); rB = raindeer.GetComponent<Rigidbody>(); } // Update is called once per frame void Update() { } private void OnTriggerEnter(Collider other) { _anim.SetBool("OpenCage", true); raindeerAnim.SetBool("Run", true); rB.velocity = new Vector3(xVel, rB.velocity.y, 0); } }
I'll try that again!
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ReindeerCage : $$anonymous$$onoBehaviour
{
Animator _anim;
Animator raindeerAnim;
public GameObject raindeer;
Rigidbody rB;
public float xVel;
// Start is called before the first frame update
void Start()
{
_anim = GetComponent<Animator>();
raindeerAnim = raindeer.GetComponent<Animator>();
rB = raindeer.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter(Collider other)
{
_anim.SetBool("OpenCage", true);
raindeerAnim.SetBool("Run", true);
rB.velocity = new Vector3(xVel, rB.velocity.y, 0);
}
}
Your answer
Follow this Question
Related Questions
i'm trying to make an fps with rigidbody but the player isn't going according to it's direction 0 Answers
How to properly move a rigidbody character? 0 Answers
Npc spawning outside the camera view and sliding down within the camera view 0 Answers
Determine the direction an object is actually moving? 0 Answers