- Home /
Because of default SmoothFollo2D script my ball is not moving smoothly. How to fix that?
Here is a code, attached to ball:
void FixedUpdate () {
if (Input.GetKey(KeyCode.RightArrow))
{
rigidbody.AddForce(Vector3.right * 5);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
rigidbody.AddForce(Vector3.left * 5);
}
}
Answer by Samael_00001 · Jul 25, 2014 at 05:40 PM
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public GameObject player;
private Vector3 offset;
// Use this for initialization
void Start () {
offset = transform.position;
}
// Update is called once per frame
void LateUpdate () {
transform.position = player.transform.position + offset;
}
}
Answer by gcoope · Jul 25, 2014 at 03:42 PM
Try
rigidybody.AddForce(-Vector3.right * 5 * Time.deltaTime);
This will add a smooth update. You could also try putting it in just Update and see how it reacts.
Answer by Nitrosocke · Jul 25, 2014 at 04:04 PM
Maybe you have luck with this code:
#pragma strict
var rotationSpeed = 100;
function Update () {
var rotation : float = rotationSpeed;
rotation *= Time.deltaTime;
if (Input.GetKey(KeyCode.LeftArrow)){
rigidbody.AddRelativeTorque (Vector3.left * rotation);
}
if (Input.GetKey(KeyCode.RightArrow)){
rigidbody.AddRelativeTorque (Vector3.right * rotation);
}
}
I tried this in Unity and it seems like the Ball moves smooth. Give it a try.
that's nice, but it seems my left became my forward and my right became my backward o_O
Answer by Vitor_r · Jul 25, 2014 at 04:03 PM
You shouldn't be listening for input on the FixedUpdate. You should listen for input on Update and apply the force on the FixedUpdate. Use a boolean to control this.
void Update(){
if (Input.GetKey(KeyCode.RightArrow)){
moveRight=true
}
}
void FixedUpdate () {
if (moveRight){
moveRight=false;
rigidbody.AddForce(Vector3.right * 5);
}
}
Your answer
Follow this Question
Related Questions
Smooth rotation... 1 Answer
A node in a childnode? 1 Answer
Character Movement jitters 1 Answer
How to smoothen movement of a child object? 1 Answer
How can i drag objects smoothly with touch? Please help me with this code! 1 Answer