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 jposuna · Dec 30, 2012 at 08:30 PM · terraincharactermoveclickheight

Click to Move problem with hills

Hi, I am using the following code to make the character move to the position clicked on.

 using UnityEngine;
 using System.Collections;
 
 public class MoveCharacter2Clicked : MonoBehaviour {
   private Transform myTransform;        // this transform
   private Vector3 destinationPosition;    // The destination Point
   private float destinationDistance;      // The distance between myTransform and destinationPosition
   private float originalMoveSpeed;
 
   public float moveSpeed;           // The Speed the character will move
 
   void Start() {
     myTransform = transform;              // sets myTransform to this GameObject.transform
     destinationPosition = myTransform.position;     // prevents myTransform reset
     originalMoveSpeed = moveSpeed;
   }
 
   void Update() {
     // keep track of the distance between this gameObject and destinationPosition
     destinationDistance = Vector3.Distance(destinationPosition, myTransform.position);
 
     // Moves the Player if the Left Mouse Button was clicked
     if ((Input.GetMouseButtonDown(0) || Input.GetMouseButton(0)) && GUIUtility.hotControl ==0) {
       Plane playerPlane = new Plane(Vector3.up, myTransform.position);
       Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
       float hitdist = 0.0f;
       Debug.Log(ray);
   
       if (playerPlane.Raycast(ray, out hitdist)) {
         Vector3 targetPoint = ray.GetPoint(hitdist);
         destinationPosition = ray.GetPoint(hitdist);
         Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
         myTransform.rotation = targetRotation;
       }
     }
     // To prevent code from running if not needed
     if(destinationDistance > .5f) {
       moveSpeed = originalMoveSpeed;
       myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
     }
     else { // To prevent shakin behavior when near destination
       moveSpeed = 0;
     }
   }
 }

It works great when the terrain is completely flat, but if I increase the height of the terrain, the character falls through, ignoring the ground and into the abyss.

Is there anything I am doing wrong?

Thank you!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TomPendergrass · Dec 30, 2012 at 10:35 PM

You mention that your character falls through the ground. is your character a rigidbody? if so, using transform.MoveTowards() will not work well with the physics engine. instead, you can try transform.rigidbody.AddForce(), which will take collisions into account.

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
0

Answer by jposuna · Dec 31, 2012 at 01:16 AM

Hi, thanks for your answer. I'm quite new to Unity, and I believe they are different things (correct me if I'm wrong), but I am using a CharacterController instead of a RigidBody. Would the same still apply?

Thank you!

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 TomPendergrass · Dec 31, 2012 at 07:58 AM 0
Share

Ah, I see. I never really used the Character Controller. It's convenient for basic functionality, but unless you can make sense of all the code in the character controller and motor scripts, it can be frustrating to modify. I'm not sure of all you're trying to accomplish, but it might be worth making your own script!

 var target : Transform;
 
 var moveSpeed : float;
 
 function Update()
 {
     transform.LookAt(target);
     rigidbody.AddRelativeForce(0,0,moveSpeed, Velocity$$anonymous$$ode.InstantVelocityChange);
 }

For this script to work it would have to be attached to an object that has a collider and rigidbody on it. It's a bit more simple than the character controller. You'd want to plug in the code you use to get where the user has clicked as well (target). Good Luck!

avatar image
0

Answer by MaGuSware™ · Dec 31, 2012 at 08:51 AM

I recommend adding a character motor onto the object with the controller.

Then look at the script "FPSInputController.js". You will notice that the script is setting the movement direction of the motor based on the control input.

You can set he motor.inputMoveDirection on yours to the direction that your character needs to move to.

I hope this is enough information for you to go off and develop a full solution to your problem.

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

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

10 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

Related Questions

Kinect issue with Character Movement while in flight 0 Answers

Can I prevent player through terrain when terrain height is modified at runtime? 1 Answer

Following Terrain Height? 2 Answers

How would I make it so that my character moves only so fast 1 Answer

Terrain help :c 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