how I can make a ship to tilt sideways when I move?

I have a problem when I move the ship with joystick it doesnt tilt…

using UnityEngine;
using System.Collections;
using UnitySampleAssets.CrossPlatformInput;
public class MoverNave : MonoBehaviour {

    float sense = 5;
    public float speed;
    public float tilt;
    // Use this for initialization
    void Start () {
  
    }
  
    // Update is called once per frame
    void Update () {

        transform.Translate (new Vector3 (0, CrossPlatformInputManager.GetAxis ("Vertical")) * Time.deltaTime * speed);
        float y = CrossPlatformInputManager.GetAxis("Mouse Y") * sense;
        transform.Rotate(0,y,0);

        transform.Translate (new Vector3 (CrossPlatformInputManager.GetAxis ("Horizontal"),0) * Time.deltaTime * speed);
        float x = CrossPlatformInputManager.GetAxis("Mouse X") * sense;
        transform.Rotate(x,0,0);

        Vector3 movement = new Vector3(CrossPlatformInputManager.GetAxis("Horizontal"), CrossPlatformInputManager.GetAxis("Vertical"), 0.0f);
        GetComponent<Rigidbody>().velocity = movement * speed;

        GetComponent<Rigidbody>().rotation = Quaternion.Euler(0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);

    }
  

}

How I can do that?

I don’t understand the problem, but I do see that you have a weird mixture of physics and non-physics movement here. You’re directly manipulating the transform position, and the rigidbody position, and the velocity too!

Please decide whether you want to use physics or no physics (I recommend no physics, unless you have a good reason why physics is necessary). And then use only the methods appropriate for that.

Or in other words: muck with the Transform (with Rigidbody removed or set to kinetic), or with the Rigidbody, but never muck with both.

1 Like

Ok sorry, Ive been changing the context of the topic because I found a solution to the problem…but Now my problem is how I can tilt the ship when I move to sideways with the joystick. Do you know that?

Yes, but how you do it depends on whether or not you’re using physics. Two completely different answers.

1 Like

You didn’t answer JoeStrout though… do you want to use physics, or no physics?

The way you might go about it differs depending, and as was suggested, mixing them can be bad.

If no physics, just tilt the rotation around the forward axis.

If physics, apply a force in that direction.

Sorry, is with no physics, but how I tilt the rotation around the forward axis with joystick…???

[quote=“JoeStrout, post:4, topic: 626281, username:JoeStrout”]
Yes, but how you do it depends on whether or not you’re using physics. Two completely different answers.
[/quote]Sorry Again…
I want to use no physics. How I can do that? If you can help me please

In that case, just do something like:

transform.rotation = Quaternion.Euler(0, 0, Input.GetAxis("Horizontal") * 30);

where 30 is the maximum number of degrees you want it to tilt.

1 Like

Thanks a lot!!! Me worked :slight_smile: