Question by
unity_Z6HWt8F-B72D-Q · Sep 29, 2019 at 03:54 PM ·
transformaddforceaxisworldspacelocalspace
Add force forward to a ball using local coordinates doesn't work as I wanted
I'm trying to control a ball for a simple game. The goal is to rotate the ball with the horizontal input, and then, pressing vertical input, add force to the ball to move forward or backward. I've been searching here, and I've found the add relative force, and also the addforce(transform.forward) to use the local axis of the ball, but, when the z axis is pointing to the floor, the ball doesn't move anymore, since I'm applying the force against the floor. I understand that I need to use some king of combination of local and global axis, but, I have no idea on how to do it. Here's the code I'm using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour
{
public float force = 10000f;
float forwardInput;
float sideInput;
public float turnSpeed;
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
GetInputs();
Vector3 movement = new Vector3(0.0f, 0.0f, forwardInput * force * Time.deltaTime);
transform.Rotate(Vector3.up * sideInput * turnSpeed * Time.deltaTime);
rb.AddForce(transform.forward * forwardInput * Time.deltaTime * force);
}
void GetInputs()
{
forwardInput = Input.GetAxis("Vertical");
sideInput = Input.GetAxis("Horizontal");
}
}
Also. I'm beginner so I would, if possible, really appreciate a quick explanation, Thank you so much in advance for your time.
Comment