Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Cataclysmix · Dec 17, 2015 at 02:39 PM · c#movementlocalpositionconstrain

Constrain local X movement

Howdy, I'm attempting to make a little skate game. And all I want to do is make it so that when the player has jumped, they continue along the same vector, but can rotate around local Y ( e.g to do a 360 ollie or whatever ). Here's what I have so far:

 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     Rigidbody playerRigidbody;
 
     float acceleration = 5, deceleration = 2;
     float currentSpeed, minSpeed = 0, maxSpeed = 20;
     float turn, turnSpeed = 2;
     float jumpForce, jumpForceModifier = 4, minJumpForce = 2, maxJumpForce = 5;
 
     Vector3 preJumpVector;
 
     void Awake()
     {
         playerRigidbody = GetComponent<Rigidbody>();
     }
 
     void Start()
     {
         jumpForce = minJumpForce;
     }
 
     void Update()
     {
         Impetus();
         Turn();
         Jump();
     }
 
     bool OnGround()
     {
         return (Physics.Raycast(transform.position, Vector3.down, .1f));
     }
 
     void Impetus()
     {
         if (OnGround())
         {
             if (Input.GetKeyDown(KeyCode.W))
                 currentSpeed += acceleration;
             else if (Input.GetKeyDown(KeyCode.S))
                 currentSpeed -= acceleration;
 
             transform.Translate(Vector3.forward * Time.deltaTime * currentSpeed);
         }
 
         else
         {            
             transform.Translate(preJumpVector * Time.deltaTime * currentSpeed);     //here's where I think it needs to change
         }
         
         currentSpeed -= deceleration * Time.deltaTime;
         currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, maxSpeed);
     }
 
     void Turn()
     {
         turn = Input.GetAxis("Horizontal");
         transform.Rotate(Vector3.up * turn * turnSpeed);
     }
 
     void Jump()
     {
         if (Input.GetButton("Jump") && jumpForce <= maxJumpForce)
         {
             jumpForce += jumpForceModifier * Time.deltaTime;
         }
 
         if (Input.GetButtonUp("Jump") && OnGround())
         {
             playerRigidbody.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
             jumpForce = minJumpForce;
             preJumpVector = Vector3.forward;   //think this should give the pre jump vector, then I want to constrain movement to this whilst not OnGround(), but still be able to rotate
         }
     }
 }
 
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
0
Best Answer

Answer by Cataclysmix · Dec 27, 2015 at 01:00 AM

I've been helped with this question elsewhere (by Sebastian Lague. Highly recommend his tutorials : https://www.youtube.com/channel/UCmtyQOKKmrMVaKuRXz02jbQ)

line 50 just needed to be transform.Translate(preJumpVector Time.deltaTime currentSpeed, Space.World);

and 74 should've been preJumpVector = transform.forward; //transform.forward, rather than Vector3.forward

so easy.... (kinda figured it'd be something real simple)

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 PabloUnityArgentina · Dec 27, 2015 at 12:58 AM

All you have to do is use the constaints of the playerRigidBody.

Just put this on your Jump function, when a jump is starting:

playerRigidbody.constraints = RigidbodyConstraints.FreezePositionX || RigidbodyConstraints.FreezePositionZ || RigidbodyConstraints.FreezePositionZ || RigidbodyConstraints.FreezeRotationX;

And put this when a jump is over (OnGround, Impetus method)

playerRigidbody.constraints = RigidbodyConstraints.None;

link text

link text

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

LeanTween.moveLocal sometimes snaps to position rather than moving over time 0 Answers

LeanTween.moveLocal sometimes snaps to position rather than moving over time 0 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