Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RaulG · Jul 22, 2013 at 10:51 PM · raycastplayerwont stop

Stop player movement if it's Raycast hits an object

I have the debug for my raycasting, and it all seems to work as intended. Now all I need is for the Player to stop moving forward if the Raycast hits another object.

Can I get examples or links to the Script References that can help me do this?

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour 
 {
     public PlayerCollision pCollision;
     
     public float speed = 10.0f;
     public float jumpSpeed = 10.0f;
     public float gravity = 20.0f;
     public float JPSpeed = 10.0f;
     public Vector3 moveDirection = Vector3.zero;
     public Vector3 jumpDirection = Vector3.zero;
     
     // Use this for initialization
     void Start () 
     {
         
     }
     
     void Jumping()
     {
         CharacterController playerControl = GetComponent<CharacterController>();
         
         jumpDirection = new Vector3(Input.GetAxis("Horizontal"),0.0f,0.0f);
         jumpDirection = transform.TransformDirection(jumpDirection);
         jumpDirection *= speed;
         playerControl.Move (jumpDirection * Time.deltaTime);
         
     }
     // Update is called once per frame
     void Update () 
     {
         //PlayerCollisionCheck Bools
         bool canMoveRight = pCollision.pbRight;
         bool canMoveLeft = pCollision.pbLeft;
         
         CharacterController playerControl = GetComponent<CharacterController>();
         if (playerControl.isGrounded)
         {
                     //Putting this here
                     if (!canMoveLeft || !canMoveRight)
         return;
                    //Kind of gets what I want. But dissallows left and right movement.

             if (Input.GetKeyDown(KeyCode.D) && canMoveRight)
                 {
                     moveDirection = new Vector3(1.0f,0.0f,0.0f);
                     moveDirection *= speed;
                 }
             if (Input.GetKeyUp(KeyCode.D))
             {
                 moveDirection = new Vector3(0.0f,0.0f,0.0f);
             }
         
             if (Input.GetKeyDown(KeyCode.A) && canMoveLeft)
             {
                 moveDirection = new Vector3(-1.0f,0.0f,0.0f);
                 moveDirection *= speed;
             }
             if (Input.GetKeyUp(KeyCode.A))
             {
                 moveDirection = new Vector3(0.0f,0.0f,0.0f);
             }
             if(Input.GetButtonDown("Jump"))
             {
                 moveDirection.y = jumpSpeed;
             }
         }
         moveDirection.y -= gravity * Time.deltaTime;
         Jumping();
         playerControl.Move (moveDirection * Time.deltaTime);
                 
     }
 }


Okay worse problem: Even though canMoveRight is true, it still doesn't allow me to move right. Probably because of that return;, but if I don't use return, the player is allowed to continue on.

Note: This is a small test for things I want to do that might need something like this later. So I could just use the Unity Collisions but there are things that Raycast can do better.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aldonaletto · Jul 22, 2013 at 11:00 PM 0
Share

This depends on your movement script - edit your question and post it.

avatar image RaulG · Jul 22, 2013 at 11:10 PM 0
Share

Currently changing my movement script to contain a bool.

But give me a sec. $$anonymous$$aybe you guys can make it easier on me!

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make "enemy" have a frustum(cone) view and not a circle radius 2 Answers

I want to click on my screen and make my character move there, having trouble with raycasts. 1 Answer

Raycast Destroys player. 1 Answer

Player takes damage on collision with an enemy 1 Answer

Top Down Shooter Player Controller 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges