- Home /
This question was
closed Apr 16, 2019 at 05:44 PM by
craftnut for the following reason:
I solved the issue myself! Yay for me!
Question by
craftnut · Apr 17, 2019 at 08:07 AM ·
scripting problemgamescripting beginnermovement scriptgameobjects
Why wont my character stop moving when it collides with the obstacles?,Why doesn't my movement script stop?
So basically I am making a game, I am a beginner watching Brackey's tutorials (see https://www.youtube.com/watch?v=gAB64vfbrhI) and it the code gives me errors and I would like help. He is the script [code=CSharp]using UnityEngine;
public class PlayerCollison : MonoBehaviour {
public PlayerMovement movement;
void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.collider.tag == "Obstacle")
{
movement.enabled = false;
} } }[/code] Here is the Movement Script [code=CSharp]using UnityEngine;
public class PlayerMovement : MonoBehaviour { // "RB" Refrences RigidBody public Rigidbody rb;
// Variable for players foward movement
public float fowardForce = 2000f;
public float sidewaysForce = 500f;
// "FixedUpdate" is for physics
void FixedUpdate()
{
// Gives the player their foward movement
// Variable "fowardForce" has a defualt value of 2000
rb.AddForce(0, 0, fowardForce * Time.deltaTime);
if ( Input.GetKey("d") )
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
}
if (Input.GetKey("a"))
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0);
}
}
}[/code] Here is a video of what happens link text
[1]: https://www.youtube.com/watch?v=gAB64vfbrhI
Comment
Follow this Question
Related Questions
Auto-move on grid and draw line problem 0 Answers
Bullets follow the ship in 2D Space Shooter game 2 Answers
Stop movement while attacking 1 Answer