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 Ochreous · Aug 08, 2013 at 07:47 PM · c#collisionplane

C# Plane Collision Detection

Hi everyone, I have a script that allows my character controller to move via left mouse click. There is one problem with my script. It doesn't detect collisions. The following below is what causes the character to move. It moves using a plane and I'm not sure how I can get the plane to detect a collision. I think what I need to do is use an if statement before the playerPlane.Raycast. But I'm not sure how to use an if statement for collision detection. Any idea how I can achieve this?

 using UnityEngine;
 using System.Collections;
 
 public class moveOnMouseClick : MonoBehaviour 
 {
     private Transform myTransform;          // this transform
     private Vector3 destinationPosition;     // The destination Point
     private float destinationDistance;        // The distance between myTransform and destinationPosition
     public float hitdist = 0.0f;
     public float moveSpeed;                 // The Speed the character will move
     public Ray rayCamera;
     public RaycastHit hitCamera;
     public RaycastHit playerHit;
     float playerHitDist = 2.0f;
 
     void Start () 
     {
        myTransform = transform;                 // sets myTransform to this GameObject.transform
        destinationPosition = myTransform.position;      // prevents myTransform reset
     }
 
     void Update () 
     {
        rayCamera = Camera.main.ScreenPointToRay(Input.mousePosition);
        // keep track of the distance between this gameObject and destinationPosition
        destinationDistance = Vector3.Distance(destinationPosition, myTransform.position);
 
        if(destinationDistance < .5f)
        {       // To prevent shakin behavior when near destination
          moveSpeed = 0;
        }
 
        else if(destinationDistance > .5f)
        {      // To Reset Speed to default
          moveSpeed = 3;
        }
 
        // Moves the Player if the Left Mouse Button was clicked
 
        if(Physics.Raycast(transform.position, transform.forward, out playerHit, playerHitDist))
          {
           //Code for your player's raycast.
          }  
 
 
        if (Input.GetMouseButtonDown(0)&& GUIUtility.hotControl ==0) 
        {
 
          if (Physics.Raycast(rayCamera, out hitCamera)) 
          {
           //Code for your camera's raycast, SEPARATE from the player's raycast code.
           destinationPosition = new Vector3(hitCamera.point.x, transform.position.y, hitCamera.point.z);
          }
        }  
 
        /*
        // Moves the player if the mouse button is hold down
        else if (Input.GetMouseButtonDown(0)&& GUIUtility.hotControl ==0) 
        {
        float playerHitDist = 2.0f;
 
        if(Physics.Raycast(transform.position, transform.forward, out playerHit, playerHitDist))
        {
        //Code for your player's raycast.
        }
 
        else if (Physics.Raycast(rayCamera, out hitCamera)) 
        {
        //Code for your camera's raycast, SEPARATE from the player's raycast code.
        destinationPosition = new Vector3(hitCamera.point.x, transform.position.y, hitCamera.point.z);
        }
        }    
        */
 
        // To prevent code from running if not needed
        if(destinationDistance > .5f)
        {
          myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
        }
     }
 }
Comment
Add comment · Show 8
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 clunk47 · Aug 09, 2013 at 03:15 AM 0
Share

I'll have to sleep on this, my brain is fried. If you don't have it figured out by tomorrow I should be able to help after I've got some rest, been a long day.

avatar image Ochreous · Aug 09, 2013 at 03:20 AM 0
Share

Ok, get some sleep and thank you for helping me with this incredibly difficult task.

avatar image clunk47 · Aug 09, 2013 at 03:28 AM 0
Share

It's probably gonna end up being something super easy lol I am just like a zombie right not. Peace out, I'll check this question when I wake up :D

avatar image Ochreous · Aug 09, 2013 at 05:20 AM 0
Share

I believe at the root of this problem is transform.position. Transform.position negates any possibility of collision because it just a straight forward teleport. I have come up with a theory. The Character Controller isn't actually moving towards the point but rather teleporting frame by frame to destinationPosition. I haven't found any substitute for transform.position that would allow for collisions. Please let me know if you have any ideas.

avatar image clunk47 · Aug 09, 2013 at 02:40 PM 0
Share

Well now that I've got some sleep, I give myself a palm to the face lol. Yeah. You are correct. You need to use the CharacterController move to do this. Otherwise ins$$anonymous$$d of a character controller, since you weren't really ever using it anyway, I'd just remove that component and add a capsule collider and rigidbody, and use rigidbody.Addforce to move your character. Collision problem solved XD

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Xtro · Aug 08, 2013 at 08:01 PM

Are you making a click and move controller like Diablo?

You have do some path finding for that purpose.

Comment
Add comment · Show 1 · Share
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 clunk47 · Aug 08, 2013 at 08:06 PM 0
Share

He's asking how to detect collisions with raycast lol.

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

C# GameObject is not detecting collision with Character Controller 2 Answers

C# Plane Detecting a Gameobject 1 Answer

How to check if an object is colliding with another from another script ? 1 Answer

C# Collision Detection Help 0 Answers

c# Adjust In-Game audio 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