|
Post by archieandrews on Feb 20, 2019 19:18:44 GMT
Hi How do I get the current locomotion state of the player from another object (i.e. get compoent) using C# ? I would like to determine if character is running or sprinting when a collision occurs.
TY
|
|
|
Post by Invector on Feb 21, 2019 16:58:24 GMT
You can simply ask if the controller bool isSprinting is true or false
public vThirdPersonInput tpInput;
void Start() { tpInput = GetComponent<vThirdPersonInput>(); }
void Update() { if(tpInput.cc.isSprinting) //do something }
|
|
|
Post by archieandrews on Feb 24, 2019 16:24:07 GMT
Hi I have a problem with the solution yu provided. I have "use instance" unselected in my controller. However when the game starts the public game objects of the player that I add (public vThirdPersonInput tpInput;) dissapear therefore it does not see them and the code errors.
See sreenshot. My code is on a soccer ball. If I add the 3rd person controller to it, it dissaperas when the game is launched.
I would have assumed if I am not using instancing this should not be a problem?
Thanks
roll a dice
|
|
|
Post by archieandrews on Feb 24, 2019 17:00:02 GMT
This is the code I use on the soccer ball
using System.Collections; using System.Collections.Generic; using UnityEngine; using Invector.vCharacterController;
public class KickBall : MonoBehaviour {
public vThirdPersonInput tpInput; private float kickForce = 20; public AudioClip impact; AudioSource audioSource; // Use this for initialization void Start () { audioSource = GetComponent<AudioSource>(); tpInput = GetComponent<vThirdPersonInput>();
} // Update is called once per frame void Update () {
}
private void OnCollisionEnter(Collision other) {
if (other.transform.tag == "Player" )//&& other.transform.GetComponent<PlayerBehaviour>().kicking) { if (tpInput.cc.isSprinting) { kickForce = 20; } else { kickForce = 10; } audioSource.PlayOneShot(impact, 1F); Vector3 direction = (other.transform.position - transform.position).normalized;
this.GetComponent<Rigidbody>().AddForce(-direction * kickForce, ForceMode.Impulse); } } }
|
|
|
Post by Invector on Feb 25, 2019 12:19:41 GMT
The line "tpInput = GetComponent<vThirdPersonInput>();" won't work if the script is not attached on the same gameObject that has the component, so you will need to access the component using the OnCollisionEnter just like you did for the PlayerBehaviour
|
|
|
Post by archieandrews on Feb 25, 2019 18:29:14 GMT
Perfect Thanks !!
|
|