Applying force when colliding with several objects
Hi, Im currently working on a game which involves moving through obstacles and not getting knocked of the platform.
However I have a problem with forces. For each obstacle i have put a variable called type which indicates which way the is pushed if they collide with that object. (0 for push to the right and 1 for push to the left.) Then I reference the script attached to the object to check the type and push the player accordingly. Now I know this isn't the most efficient/understandable way of doing but I just want to make this work before improving efficiency. I suck at coding.
------------------------------------------------Obstacle code--------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class obstacle : MonoBehaviour
{
//static varaibles
public int type = 0;
public static bool knock = false;
public static bool knock2 = false;
void OnTriggerEnter()
{
if (type == 0)
{
knock = true;
}
if (type == 1)
{
knock2 = true;
}
}
}
-------------------------------------------Player code--------------------------------------------------------------------
void OnTriggerEnter()
{
if (obstacle.knock2 == true)
{
rb.AddForce(-push * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
Debug.Log("trigger");
if (obstacle.knock== true)
{
rb.AddForce(push * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
Now for the problem, When I pass through the first object i move like i should, i get knocked to the right and all is good. However when then hitting the second object nothing happens. If i miss the first object and hit the second one first then i move to the left like i should but all other objects after that do nothing apart from the ones of the same type. e.g if i hit an object that moves me to the right all the left objects will have no effect on me but the other right objects will. I think this is because the forces cancel each other out.I have tried using all the force types ; force, acceleration, impluse and VelocityChange but none solve this problem. I have tried moving the player directly when hitting an object with: rb.transform.Translate(Vector3.left push Time.deltaTime); for when rb =rigidbody this kinda works but this is not smooth, clunky and with forces it just looks better. Can anybody help?
you shouldn't use Time.deltaTime
outside of Update()
. Have you tried a flat value?
@$$anonymous$$atthewj866 I got rid of the Time.deltaTime in the trigger yet I'm still seeing the same results. I get pushed to the side by the first object i go through then when i go through the rest nothing happens.
Answer by Partylikeadragon · May 30, 2017 at 01:43 AM
I got I now!!!!!!!!!!!!!!! I wasn't setting the static bool equal to false once i had applied the force so the player would experience the same force from the first trigger. I was also doing Time.deltaTime in the void method so that ruined it as well. Everythings working the way it should be now. Thanks!!!
Your answer
Follow this Question
Related Questions
OnTrigger Problems Between Colliders Issue 1 Answer
How to have a game object register its OnTriggerEnter function? 1 Answer
Inactive script still catching collision/trigger 1 Answer
What is the different between OnTriggerEnter and OnCollisionEnter ? 2 Answers
ANIMATION TRIGGER DOESN'T PLAY 1 Answer