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 kadealicious · Apr 13, 2015 at 01:10 PM · c#raycastgravity

Gravity by raycasting almost working

I have been working on a project in Unity, in which I was using a raycast to detect the normal of the ground below the player. The player would then have it's up rotation set to the hit.normal of the raycast. This worked perfectly for the top half of a sphere world. When you go to the bottom half of the sphere, the player would start jerking around when the normal value changed. If you ever reached the bottom of the sphere, the player would jerk around uncontrollably, so that you couldn't move in the direction that you wanted to.

The code used for the player movement:

using UnityEngine; using System.Collections;

public class GravityRaycasting : MonoBehaviour {

 Rigidbody myBody;

 public RaycastHit rayHit;

 // Use this for initialization
 void Start () {

     myBody = this.GetComponent <Rigidbody> ();

 }
 
 // Update is called once per frame
 void FixedUpdate () {

     Physics.Raycast (transform.position, -transform.up, out rayHit, Mathf.Infinity);

     transform.up = Vector3.Lerp (transform.up, rayHit.normal, 0.25f);

     myBody.AddForce (-transform.up * 50f);

     Debug.DrawLine (transform.position, rayHit.point, Color.green);

     myBody.AddForce (Camera.main.transform.forward * Input.GetAxisRaw ("Vertical") * Time.deltaTime * 560f);
     myBody.AddForce (Camera.main.transform.right * Input.GetAxisRaw ("Horizontal") * Time.deltaTime * 560f);

 }

}

Here is the mouse look script, but I doubt that this could be causing the problem:

using UnityEngine; using System.Collections;

public class MouseLook : MonoBehaviour {

 public float lookSensitivity = 5f;
 
 public float yRotation;
 public float xRotation;
 
 float currentYRotation;
 float currentXRotation;
 
 public float yRotationV;
 public float xRotationV;
 
 public float lookSmoothDamp = 0.1f;
 
 public bool UseYAxis = true;
 public bool UseXAxis = true;

 public GameObject playerObject;

 void FixedUpdate () {

     if (UseYAxis) {

         xRotation = Mathf.Clamp (xRotation, -90f, 90f);
     }
 }
 
 void Update () {

     if (UseXAxis) {
         
         yRotation += Input.GetAxis ("Mouse X") * lookSensitivity;

         currentYRotation = Mathf.SmoothDamp (currentYRotation, yRotation, ref yRotationV, lookSmoothDamp);
     }
     
     if (UseYAxis) {
         
         xRotation -= Input.GetAxis ("Mouse Y") * lookSensitivity;
         
         currentXRotation = Mathf.SmoothDamp (currentXRotation, xRotation, ref xRotationV, lookSmoothDamp);
     }

     transform.localRotation = Quaternion.Euler (currentXRotation, currentYRotation, 0f);

 }

}

Any help would be appreciated.

Thanks.

Comment
Add comment · Show 1
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 AlwaysSunny · Apr 13, 2015 at 04:22 AM 0
Share

"This worked perfectly for the top half of a sphere world. When you go to the bottom half of the sphere, the player would start jerking around..."

It's not totally clear what you're doing or what you desire, but you are obviously creating a specialized "world" and corresponding physics behaviors. Your solution should not care about any object's orientation in world space. This implies there should be no consideration of conventional "up, down, etc" directions. Everything must rely upon the actor's local orientation. This will take some careful planning and consideration. How to best approach this requirement depends upon your desires and the nature of the project. This is not sufficiently elaborated upon here, so I wouldn't expect anyone to intuit what you're trying to accomplish.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DHARMAKAYA · Oct 05, 2017 at 10:18 PM

Hi,

Any update? Thanks in advance!

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 DHARMAKAYA · Oct 05, 2017 at 10:20 PM 0
Share

PS: Tried turning off the Y gravity in the physics settings of the project?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast not working on object without gravity 0 Answers

Multiple Cars not working 1 Answer

How to disable gravity on an object hit with a raycast? 2 Answers

Distribute terrain in zones 3 Answers

Modifying the HeadLook Controller in C# 2 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