- Home /
Question by
GongagaSwordsman · Mar 31, 2019 at 07:43 AM ·
transform.positiondirectionobjectsspawningfacing
How do i make a "summon" object - and let the player face in its direction?
So, i have this child object called "heartcube" - it's basically a cube that my player should be able to spawn to its left\right in order to reach upper platforms on my level. Right now it's just in the scene near the player, but i want the player to be able to "summon" it? and throw it away whenever he wants. And whenever he summons it he should be facing in the direction the cube has spawned, i thought i could rotate the player on the Y axis in someway for this, but since i'm very new to unity and programming i'm nt sure how to approach this, (this is a shared project with a friend, too, so we have tasks). Hope you can help me out somehow. Here's the code for the player movement :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class Movement : MonoBehaviour {
protected Rigidbody rb;
[HideInInspector]
public bool grounded;
[Range(1, 2)]
public int playerNumber;
[SerializeField]
protected float speed;
[SerializeField]
protected float jumpSpeed;
[SerializeField]
protected float gravity = 9.81f;
[SerializeField]
protected float fastFallMultplr = 1.25f;
[SerializeField]
protected float longJumpMultplr = 0.75f;
[SerializeField]
protected float airMultiplier = 0.85f;
// Use this for initialization
void Awake () {
rb = gameObject.GetComponent<Rigidbody>();
rb.useGravity = false;
}
// Update is called once per frame
void Update () {
grounded = Physics.Linecast(gameObject.transform.position - Vector3.down * 0.1f, gameObject.transform.position + Vector3.down * 0.1f,1<< LayerMask.NameToLayer("Environment"));
float xAxis = Input.GetAxis("Horizontal_P" + playerNumber.ToString());
float jump = Input.GetAxis("Jump_P" + playerNumber.ToString());
float fire = Input.GetAxis("Fire_P" + playerNumber.ToString());
float multiplier = 1f;
if (!grounded) {
multiplier = airMultiplier;
}
float yVelocity = rb.velocity.y;
rb.velocity = (multiplier*speed * xAxis * Vector3.right) + (yVelocity * Vector3.up);
if (grounded && jump>0) {
rb.velocity= multiplier * rb.velocity.x * Vector3.right + (jump * jumpSpeed * Vector3.up);
}
//gravity
if (rb.velocity.y >= 0) {
if (!grounded && jump > 0) {
rb.AddForce(0, -gravity*longJumpMultplr, 0);
}
else {
rb.AddForce(0, -gravity, 0);
}
}
if (rb.velocity.y < 0) {
rb.AddForce(0, -gravity*fastFallMultplr, 0);
}
}
}
Comment