- Home /
edit a variable as a result of a collision
I have looked at this question for quite some time. http://answers.unity3d.com/questions/346593/editing-a-variable-from-another-script-on-collisio.html I would like to change the jumpForce variable when the player jumps off of certain materials. They are tagged with mostly colors. Here is the first script. PlayerMovement.cs
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
private float speed = 10f;
public float jumpForce = 8f;
private float gravity = 30f;
private Vector3 movedir = Vector3.zero;
void Update ()
{
CharacterController controller = gameObject.GetComponent<CharacterController> ();
if (controller.isGrounded) {
movedir = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
movedir = transform.TransformDirection (movedir);
movedir *= speed;
if (Input.GetButtonDown ("Jump")) {
movedir.y = jumpForce;
}
}
movedir.y -= gravity * Time.deltaTime;
controller.Move (movedir * Time.deltaTime);
}
}
and the collision script. Collision.cs
using System.Collections; using UnityEngine;
public class Collision : MonoBehaviour {
public float beigeJump = 28f;
void OnCollisionEnter (Collision col)
{
if (col.gameObject.tag == "beige")
{
col.gameObject.transform.GetComponent<PlayerMovement>()
.jumpForce += beigeJump;
}
}
}
Thanks
What is the problem exactly? Does the jumpForce change in the inspector when you collide? $$anonymous$$aybe your collision just isn't triggering.
Your answer
Follow this Question
Related Questions
Using variables from colliders across scripts 1 Answer
Jumping on top of an enemy problem 1 Answer
Box collider didn't jump with player 1 Answer
Adding varibales from other scripts 3 Answers