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 Mattchuuu · May 24, 2012 at 10:27 PM · collisionmovementplayerwalls

Click-to-move questions

Hey all hope some of you might be able to solve these problems I'm having with Click-to-move movement.

  1. My character is going through walls, and using a rigidbody makes it rotate in all sorts of directions when it moves(though it does stop the character from going through the walls), to make it not rotate at all I have to freeze all of the rotations on the rigidbody but by doing this the character goes through walls again. What can I do to fix this? script - http://unifycommunity.com/wiki/index.php?title=Click_To_Move

  2. I want to do a Runescape or League of Legends type path system where when the player's location and the clicked location have obstacles, a path is drawn to take the character around the obstacles. I have absolutely no idea how to go about doing this.

Thanks to all willing to help me!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · May 25, 2012 at 12:56 AM

The script ClickToMove is intended to move objects without checking collisions. If you add a rigidbody to the object, it will react to collisions and spin, bounce etc. A better solution could be to add a CharacterController to the character - but the script should be modified to use SimpleMove, or the collisions would not be checked:

var speed: float = 5; // Determines how quickly object moves towards position

private var targetPosition:Vector3; // current target pos private var character: CharacterController;

function Start(){ character = GetComponent(CharacterController); targetPosition = transform.position; }

function Update () { if(Input.GetKeyDown(KeyCode.Mouse0)){ // create a logical horizontal plane at the player position: var playerPlane = new Plane(Vector3.up, transform.position); var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hitdist = 0.0; // find point clicked in the plane (if any): if (playerPlane.Raycast (ray, hitdist)) { // update targetPosition to the clicked point: targetPosition = ray.GetPoint(hitdist); var dir = targetPosition - transform.position; dir.y = 0; // keep only the horizontal direction transform.rotation = Quaternion.LookRotation(dir); } } // always try to move the character to targetPosition: character.SimpleMove(dir * speed); // move taking gravity into account } This script makes the character continuously try to reach the point defined by the variable targetPosition; when you click anywhere in the scene, it creates a temporary logical horizontal plane at the character position and finds the clicked point, if any, and assign it to targetPosition, thus the character moves to this new position. If no valid point is clicked (when you click the sky, for instance) targetPosition isn't modified, thus the character doesn't change direction.
NOTE: You must remove the rigidbody from your character and add a CharacterController to it (menu Component/Physics/Character Controller).
NOTE 2: Your second question is way more complicated: you should use some pathfinding script, like AStar or the built in Unity pathfinding resource (Pro required).

Comment
Add comment · 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
1

Answer by rubenpvargas · May 24, 2012 at 11:49 PM

first.- Sorry for bad english

second.- im not sure if this is the best solution

Maybe you can use a linecast (a ray that go to point A to point B) to check obstacles in the way (from your caracther to the destination). when you have detected the obstacles, you can add a "navigation point" or something like that.

For example:

If linecast hit "wall"

then move to "wallNavigationPoint"

then move to final destination

Bassed on this, you can make your code more and more complex to create a very nice movement

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 Mattchuuu · May 28, 2012 at 02:47 AM 0
Share

This is very interesting, I'm going to have to give this a try!

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

6 People are following this question.

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

Related Questions

Dive collide 2 Answers

How to hold objects in third person? 1 Answer

Issues Making Character Move With Rigidbody 1 Answer

Collisions causing Rigidbody to go Haywire and move randomly. 1 Answer

Player is moving through walls when force is added from a knockback script? *UNANSWERED* 3 Answers


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