- Home /
Mario Galaxy -ish controls problem
Hi there!
I am prototyping a Mario Galaxy style ("faux") gravity system. While the attraction force didn't give me much trouble, the controls are giving me a headache.
You can move. You can jump. But when you do both things simultaneously, everything goes whack.
This is my bare-bones code:
 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     private float moveSpeed = 15;
     private Vector3 moveDirection;
 
     void Start() {
 
     }
 
     void Update() {
         moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical")).normalized;
     }
 
     void FixedUpdate() {
         rigidbody.MovePosition (rigidbody.position + transform.TransformDirection (moveDirection) * moveSpeed * Time.deltaTime);
     }
     
     void OnCollisionStay (Collision col)
     {
         if (Input.GetKeyDown (KeyCode.Space))
             rigidbody.velocity += transform.up * 20;
     }
 }
 
Any leads?
Thanks a lot in advance!
Answer by iwaldrop · Jul 18, 2014 at 11:36 PM
You don't explain what is actually happening when things "[go] whack", but you probably shouldn't be modifying the velocity of the rigidbody outside of the FixedUpdate loop. You should also be getting your control input in Update.
 void Update()
 {
     moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical")).normalized;
     isJumping = Input.GetKeyDown (KeyCode.Space);
 }
 void FixedUpdate() 
 {
     rigidbody.MovePosition (rigidbody.position + transform.TransformDirection (moveDirection) * moveSpeed * Time.deltaTime);
     if (isJumping)
         rigidbody.velocity += transform.up * 20;
 }
  
 void OnCollisionEnter (Collision col)
 {
     // if (col is ground)  -- only set isJumping to false if we touch the ground
         isJumping = false;
 }
 
 bool isJumping;
You might also consider using rigidbody.AddForce instead of modifying the velocity directly.
I will try that, thanks!
When I said it "goes whack", I meant something I couldn't explain easily. I will try now, as I am not a native English speaker.
I jump while moving, and it moves smoothly in the air, but when I stop pressing the keyboard arrows, the player starts moving in the opposite direction (in the air too).
Nope, it's still going the opposite direction when I stop pressing the button.
Your answer
 
 
             Follow this Question
Related Questions
Implementing FPS view into locomotion planet walk 1 Answer
Custom gravity based on normal vector and rigidbody.addForce 2 Answers
Character Controller and Camera for Faux gravity..(Mario Galaxy) 3 Answers
Faux Gravity, spinning at bottom of sphere 0 Answers
Click to move player while avoiding jitter at edges 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                