- Home /
Sphere control
What can I use to move a simple sphere despite of AddForce and simple transform.Translate throw Axis input?
I ask this question because both approaches have there own limitations. For example AddForce seems not very sensitive to Input. Translate vice versa very sensitive but if your sphere flips over axis changes and ypu start moving in the wrong directions.
if you want to keep moving in the same direcition with translate, calculate the vectors in a parent object and then convert them to local space of the object you want to move
Answer by Tom507 · Dec 16, 2017 at 04:14 PM
You can use the addforce = the Rigidboddy in ways that are verry controllable, by limiting the max velocity and increasing the drag and force that is applyed, there are other ways of making it move but i believe the simplest is this. here is a simple fps controller example :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FPS_Controller : MonoBehaviour {
public float Accellaration = 100;
public float maxSpeed = 2;
public float JumpHeight = 3;
public float MouseSpeed = 3;
public GameObject Camera; // you need to set this to the camera you want to use
public float Zoffset= 90;
private float LookAngleX = 0;
private float LookAngleY = 0;
private Rigidbody rb;
private Vector3 movement;
private Vector2 XYmovement;
private float Jump;
void Start () {
rb = gameObject.GetComponent<Rigidbody>();
}
void FixedUpdate (){
//###################### Movement ###############################
float moveVert = Input.GetAxis ("Vertical");
float moveHor = Input.GetAxis ("Horizontal");
XYmovement = new Vector2 (rb.velocity.x, rb.velocity.z);
if( XYmovement.magnitude > maxSpeed)// clamping speed to max speed
{
XYmovement = XYmovement.normalized * maxSpeed;
rb.velocity = new Vector3 (XYmovement.x, rb.velocity.y, XYmovement.y);
}
if (Input.GetButtonDown ("Jump")) {
Jump = JumpHeight;
}
movement = new Vector3 (moveVert, Jump, -moveHor);
movement = transform.TransformDirection (movement);
rb.AddForce (movement*Accellaration);
//##################### Mouse Controll ##########################
// Taking controller mouse input
var Xin = Input.GetAxis ("Mouse X");
var Yin = Input.GetAxis ("Mouse Y");
LookAngleX += Xin * MouseSpeed;
LookAngleY += Yin * MouseSpeed;
LookAngleY = Mathf.Clamp (LookAngleY, -70f, 89f);
Camera.transform.localEulerAngles = new Vector3(-LookAngleY, Zoffset, 0f);
transform.eulerAngles = new Vector3(0f, LookAngleX, 0f);
Jump = 0f;
}
}
you can just slap this on a ball with a rigidbody component place a camera over it and constrain the rigidbody rotation (all axis) in the drop down in editor, drag and drop the camera object on your ball in your hirarchy (make it a child of the ball) and in the slot in the script that is meant for it and you are done.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Guiding a sphere 2 Answers
Move 3D rigidbody sphere in the direction of it's rotation, controlled by the mouse. 0 Answers
Making a torque-based sphere controller by simple mouse inputs 2 Answers
Ways to make a ball delivery system, based on a back/down swing. 0 Answers