I’m about to tear my hair out! I’ve been googling for legitimately 2 hours and the answer seems to be in “Rigidbody.Velocity.y” - but, when I try to use it, I get an error “cannot modify type return value of unityengine.rigidbody.velocity”… Okay - Let me back up… sorry.
Hi All! - starting work on my second game (ever) and starting off with a simple playerController script (or so I thought…) my player Object is a sphere, and I’ve added a way to move it around on the X&Z axis’s - that was nice and easy! - but then, I went to add a jump… I didn’t realize the issues I was going to run into.
Because of the way I have scripted my Jump - It just moves upwards on the Y axis, except as the ball runs around - the Y axis’s “up” changes, since my ball rotates. After some googling - people were saying using “Vector3.up” would set the “up” direction in accordance to the Words up - so, I thought that was going to work! … it didn’t, afterwards I found out that using Rigidbody.velocity was working for people in tutorials - I really wasn’t sure WHY it was working for them, but, it was so I went to try, and I got the error “cannot modify type return value of unityengine.rigidbody.velocity” (as stated above).
So - now I’m completely lost… and ANY help would be massively appreciated - I know this is a noob question, and I KNOW there are lots of other answers on them, but most of those answers either suggest Vector3.Up or “Velocity” either way - it’s just not working…
Posting my script below. PS: sorry for the essay, but I’m extremely frazzled!
using UnityEngine;
using System.Collections;
public class playerController : MonoBehaviour {
public int speedXZ; // speed var for 2D movement
public int speedY; // speed var for 3D movement
private Rigidbody rb;
private Vector3 movement2D;
private Vector3 movement3D;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
// 2D movement starts
float horizontalAxis = Input.GetAxis("Horizontal");
float verticalAxis = Input.GetAxis("Vertical");
Vector3 movement2D = new Vector3(horizontalAxis, 0.0f, verticalAxis);
rb.AddForce((movement2D * speedXZ) * Time.deltaTime);
// 2D movement ends
//3D movement starts
Vector3 movement3D = new Vector3(0.0f, 1.0f, 0.0f);
if (Input.GetKey(KeyCode.Space) == true)
{
//gameObject.transform.Translate((movement3D * speedY) * Time.deltaTime);
//rb.velocity.y = 5;
}
//3D movement ends
}
}