- Home /
 
 
               Question by 
               aa1501 · Jun 27, 2018 at 10:47 AM · 
                colliderfps controller  
              
 
              Hi,I am new to unity..creating FPS and when collider found it should stop in that direction only..please help me.Thanks.. Below is my script-
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class moveplayer : MonoBehaviour { private Rigidbody player;
 public float speed;
 public Camera mcamera;
 bool movef = true;
 bool moveb = false;
 bool movel = false;
 bool mover = false;
 bool found = false;
 Vector3 move;
 void Awake()
 {
     player = GetComponent<Rigidbody>();
 }
 // Update is called once per frame
 void Update()
 {
    
     RaycastHit hit;
     Ray ray = new Ray(mcamera.transform.position, mcamera.transform.forward * 0.2f);
     Debug.DrawRay(mcamera.transform.position, mcamera.transform.forward * 0.2f, Color.red);
     if (Physics.Raycast(ray, out hit))
     {
         Debug.Log(hit.collider.name);
         if (hit.collider.tag == "r1")
         {
             Debug.Log(hit.collider.tag);
             movef = false;
         }
         else movef = true;
          if(movef==true)
         {
             movement();
         }
         if (movef == false)
         {
             moveb = true;
             movel = true;
             mover = true;
         }
         }
 }
 private void movement()
 {
     if(movef == true && Input.GetKey(KeyCode.UpArrow) )
     {
         transform.position += Vector3.forward * Time.deltaTime*speed;
     }
     if (moveb == true && Input.GetKey(KeyCode.DownArrow))
     {
         transform.position += Vector3.back * Time.deltaTime * speed;
         movef = true;
     }
     if (movel== true && Input.GetKey(KeyCode.LeftArrow))
     {
         transform.position += Vector3.left * Time.deltaTime * speed;
     }
     if (mover == true && Input.GetKey(KeyCode.RightArrow))
     {
         transform.position += Vector3.right * Time.deltaTime * speed;
     }
 }
 
               }
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
Internal collisions 1 Answer
how to change physics material of a colider in runtime 5 Answers
Problem with the collider(I think) 0 Answers