- Home /
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
}
}
}
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)
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
Your answer
Follow this Question
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