Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Kinenan · Jun 12, 2010 at 01:43 AM · terraingravitythird-personpoint-and-click

3rd person click to move; issues following terrain.

I have been combining various JavaScript code and sources form numerous sites. My code allows me to orbit the player with a right mouse click, and the move code for the player moves from current to the location provided from raycast/camera/terrain at mouse cursor even after letting off of the mouse button, the issue is rigidbody does not keep player on the ground and when on the ground player vibrates.

var dir : Vector3; var hit : RaycastHit ; var rot = 0; var k : Vector3; var speed : float = 10.0; var gravity : float = 5.0;

private var yourCharacterController : CharacterController;

function Start() { yourCharacterController = GetComponent(CharacterController); }

function FixedUpdate() {
if (Input.GetMouseButton(0)) { var ray = Camera.main.ScreenPointToRay(Input.mousePosition); Physics.Raycast(Camera.main.transform.position,ray.direction,hit,1000); hit.point.y += 0.1; dir = hit.point - transform.position;

     var angle = Vector3.Angle(dir, transform.forward);
     var k = Vector3.Cross(transform.forward, dir);
     k.Normalize();

     rot = k[1]*(angle*2.5);

     transform.Rotate(Vector3.up*rot*Time.deltaTime*5);
     yourCharacterController.Move(dir.normalized * Time.deltaTime * speed);
     print ("Transform Pos" + transform.position + "Dir" + dir.normalized + "Hitpoint" + hit.point);

 }

else { // if not at clicked location continue moving there if (transform.position != hit.point) { dir = hit.point - transform.position;

         angle = Vector3.Angle(dir, transform.forward);
         k = Vector3.Cross(transform.forward, dir);
         k.Normalize();

         rot = k[1]*(angle*2.5);

         transform.Rotate(Vector3.up*rot*Time.deltaTime*5);
         yourCharacterController.Move(dir.normalized * Time.deltaTime * speed);
         print ("Transform Pos" + transform.position + "Dir" + dir.normalized + "Hitpoint" + hit.point);

     }
 }

}

function OnControllerColliderHit(collision: ControllerColliderHit) { if (collision.transform.tag != "ground") { transform.position.y -= gravity * Time.deltaTime; } }

Any help would be greatly appreciated.

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 Mike 3 · Jun 12, 2010 at 10:56 AM

You mention that you're using a rigidbody, but all your code points to you using a character controller object. Using both together is usually a very bad idea, though it's permissable if the rigidbody is set to kinematic (So that it's only used for collision response - though I'm not sure you need that here)

Secondly, I'd make sure that you don't apply any vertical motion to the character. Quickest way is to change this line

hit.point.y += 0.1;

to this

hit.point.y = transform.position.y;

Thirdly, the gravity part of your code just looks crazy - It only applies gravity when you're hitting an object which isn't the ground. I'd check how the first person controller script does gravity (it applies it every frame that you're not on the ground)

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
Best Answer

Answer by Kinenan · Jun 12, 2010 at 11:23 AM

Thanks Mike. I forgot I had that line of code in there, been trying everything under the sun for about 8 or more hours and finally found something almost flawless. The only slight issue is that now my player controlled object slips slightly under the terrain briefly now and again, which the CharacterController.move function should not push an object past a collision, so a little confused as to why it's happening. Other then that its great.

I went through and added the best comments I could for my level of understanding of the code. Also added layer masking to the ray cast so it only detects object in my "Walk Mesh" layer. I am going to move onto creating a minimal particle/projector to the location clicked, and a "No walk mesh" layer masking possibly incorporating a function to fade out the top of tall objects (think NWN). If anyone sees anywhere it can be improved please let me know.

var dir : Vector3; var k : Vector3; var hit : RaycastHit ; var rot = 0; var speed : float = 10.0; var gravity : float = -18.0;

private var layerMask : int = 1 << 9; private var gravityPower : Vector3 = Vector3.zero; private var yourCharacterController : CharacterController;

function Start() { yourCharacterController = GetComponent(CharacterController); gravityPower.y = gravity; }

function FixedUpdate() {
if (Input.GetMouseButton(0)) { // Cast ray from mouse to point on terrain and get location var ray = Camera.main.ScreenPointToRay(Input.mousePosition); Physics.Raycast(Camera.main.transform.position,ray.direction,hit, 1000,layerMask); //hit.point.y += 0.1;

     // Define direction to move
     dir = hit.point - transform.position;
     // Get rotation smoothly
     var angle = Vector3.Angle(dir, transform.forward);
     var k = Vector3.Cross(transform.forward, dir);
     k.Normalize();
     rot = k[1]*(angle*2.5);

     // ---- Move controller ---- //

     // Apply Gravity
     yourCharacterController.Move(gravityPower * Time.deltaTime);
     // Apply Rotation
     transform.Rotate(Vector3.up*rot*Time.deltaTime*5);
     // Move towards direction (dir) normalized
     yourCharacterController.Move(dir.normalized * Time.deltaTime * speed);
     // Debug check
     print ("Transform Pos" + transform.position + "Dir" + dir.normalized + "Hitpoint" + hit.point);

 }

else { // if not at clicked location continue moving there if (transform.position != hit.point) { // Define direction to move dir = hit.point - transform.position; // Get rotation smoothly angle = Vector3.Angle(dir, transform.forward); k = Vector3.Cross(transform.forward, dir); k.Normalize(); rot = k[1]*(angle*2.5);

         // Move controller

         // Apply Gravity
         yourCharacterController.Move(gravityPower * Time.deltaTime);
         // Apply Rotation
         transform.Rotate(Vector3.up*rot*Time.deltaTime*5);
         // Move towards direction (dir) normalized
         yourCharacterController.Move(dir.normalized * Time.deltaTime * speed);
         // Debug check
         print ("gravityPower" + gravityPower.y);

     }
 }

}

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

No one has followed this question yet.

Related Questions

Character remains floating after walking up higher terrain. 2 Answers

object flying around randomly 0 Answers

Why I can't see convex meshes in unity? 0 Answers

Make a simple tree 1 Answer

Why does my camera's sphere collider pass thru terrain? 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